I would like to know if a React component extending React.Component
deeply compares an object when trying to decide if it needs to rerender.
For instance, given
const Foo = ({ bar }) => {
return <div>{bar.baz}</div>
}
class App extends React.Component {
constructor() {
super()
this.state = { bar: { baz: 1} }
}
render() {
return <Foo bar={this.state.bar} />
}
}
If inside App
, the state bar
changes to {baz: 2}
, does <Foo />
deeply compare the previous prop bar
and the newly received prop?
Incidentally, the docs for PureComponent says
Extend PureComponent ... Or, consider using immutable objects to facilitate fast comparisons of nested data.
But does not go into much more details. Any ideas?
No, by default Component will re-render even if its props stay the same (by doing no compare at all), unless you decide to implement your own shouldComponentUpdate .
There's a checkbox well hidden in the React DevTools settings that allows you to visually highlight the components that rerendered. To enable it, go to "Profiler" >> click the "Cog wheel" on the right side of the top bar >> "General" tab >> Check the "Highlight updates when components render." checkbox.
Props are not affected by the state change, so heavy components won't re-render.
React schedules a render every time the state of a component changes. Scheduling a render means that this doesn't happen immediately. React will try to find the best moment for this. Changing the state means that React triggers an update when we call the setState function (in React hooks, you would use useState ).
Unless you implement a shouldComponentUpdate
lifecycle method yourself, a component extending React.Component
won't compare the props before going through a re-render cycle where it compares the previous and current Virtual DOM to decide what to re-render
Also a component extended using React.PureComponent
doesn't deeply compare props with prevProps and state with prevState but performs shallow comparison to decide, if at all a re-render is needed or not.
For a Functional component
from v16.6.0 onwards, React has introduced a Higher Order Function, React.memo
which can be used to make a functional component behave as a React.PureComponent
According to the docs:
React.memo
is a higher order component. It’s similar toReact.PureComponent
but for function components instead of classes.It can be used like
const MyComponent = React.memo(function MyComponent(props) { /* render using props */ });
If your function component renders the same result given the same props, you can wrap it in a call to React.memo for a performance boost in some cases by memoizing the result. This means that React will skip rendering the component, and reuse the last rendered result.
By default it will only
shallowly compare complex objects
in the props object. If you want control over the comparison, you can also provide a custom comparison function as the second argument.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With