1

This problem is hitting on my performance badly. On each state change, my children's are re-rendered. Is there any way to get rid of this? How can I prevent my child components to not get affected from this type of re-renders. Please help.

 import React from "react"; const Content = ({children}) => { console.log("I am rendering multiple times on each update"); return children; }; export default function App() { const [open, setOpen] = React.useState(false); return ( <> <button onClick={() => setOpen(!open)}>{open ? "Close" : "Open"}</button> <Content> <p>test-children</p> </Content> </> ); } 
5
  • React.memo for Content Commented Jun 12, 2020 at 12:47
  • @HMR Tried that but it didn't help because sometimes I want to pass children props on Content where memo doesn't support Commented Jun 12, 2020 at 12:58
  • 1
    @AnugrahaAcharya You may find this detailed answer by Dan Abramov helpful. Commented Jun 12, 2020 at 13:13
  • 1
    @AnugrahaAcharya I can't open this question again but you are correct, the component will re render if it has children even if all children themselves are pure components. Maybe you could create a new question specifically asking how to prevent re render when using children. I think that if you're using children then don't have the component do other heavy calculations or rendering, when children are pure then you should not have a performance problem. Commented Jun 12, 2020 at 14:13
  • Thanks @HMR. it helped me. I end up with something like codesandbox.io/s/prevent-renders-on-sidebar-toggle-lmo4j?file=/… Commented Jun 13, 2020 at 5:23

2 Answers 2

2

Use React.memo which is a higher order component that returns a new memoized component.

The memoized component will not trigger a re-render if it's props haven't changed.

import React from "react"; const Content = () => { console.log("I am rendering multiple time on each open sidebar"); return <div>test-content</div>; }; const MemoizedContent = React.memo(Content); export default function App() { const [open, setOpen] = React.useState(false); return ( <> <button onClick={() => setOpen(!open)}>{open ? "Close" : "Open"}</button> <MemoizedContent /> </> ); } 

You can learn more about React.memo here

Sign up to request clarification or add additional context in comments.

Comments

0

To avoid re-rendering you should use React.memo in the component Content component as follows:

const Content = React.memo(() => { ... }); 

This way the component will shallowly compare the props and only render again when properties changes, which in this case doesn't.

1 Comment

I end up with something like this. By using react-fast-compare with react.memo codesandbox.io/s/prevent-renders-on-sidebar-toggle-lmo4j?file=/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.