Log shows that my parent component is rerendering itself.
But the child component's render method is not getting called.
I thought child rerenders with the following logic, and I think I'm wrong about it. How does react decide which child components gets rerendered when parent rerenders?
shouldComponentUpdate
gets calledParent render looks like
render() {
let { viewConfig } = this.props
console.log("ViewConfigSettingBase rendering")
return (
<div>
{
Object.keys(viewConfig.availableSubviewConfigMap).map((sectionName, index) => {
var subviewConfigData = viewConfig.availableSubviewConfigMap[sectionName]
return (
<ViewConfigSettingRow
key={sectionName}
viewConfigData={subviewConfigData}
sectionName={sectionName}
parentViewConfig={viewConfig}
/>
)
})
}
</div>
)
}
Now, we know that React components re-render themselves and all their children when the state is updated. In this case, on every mouse move the state of MovingComponent is updated, its re-render is triggered, and as a result, ChildComponent will re-render as well.
In order for props to change, they need to be updated by the parent component. This means the parent would have to re-render, which will trigger re-render of the child component regardless of its props.
No, it will not re-render. If you pass any props to the component from the parent component and you update that prop in children or that prop update in the parent component so both will re-render.
As a result, the child components only update when the parent component's state changes with one of those functions. Directly mutating the props object is not allowed since this won't trigger any changes, and React doesn't notice the changes.
React re-renders on a props or state change. If you extended a PureComponent the children will do a check if the props have changed. If yes -> shouldComponendUpdate will return true otherwise false. Maybe this is the case?
Component lifecycle: https://facebook.github.io/react/docs/react-component.html
Updating
An update can be caused by changes to props or state. These methods are >called when a component is being re-rendered:
componentWillReceiveProps()
shouldComponentUpdate()
componentWillUpdate()
render()
componentDidUpdate()
shouldComponentUpdate()
Use shouldComponentUpdate() to let React know if a component's output is >not affected by the current change in state or props. The default >behavior is to re-render on every state change, and in the vast majority >of cases you should rely on the default behavior.
shouldComponentUpdate() is invoked before rendering when new props or >state are being received. Defaults to true This method is not called for >the initial render or when forceUpdate() is used.
Returning false does not prevent child components from re-rendering when >their state changes.
Currently, if shouldComponentUpdate() returns false, then >componentWillUpdate(), render(), and componentDidUpdate() will not be >invoked. Note that in the future React may treat shouldComponentUpdate() >as a hint rather than a strict directive, and returning false may still result in a re-rendering of the component.
If you determine a specific component is slow after profiling, you may >change it to inherit from React.PureComponent which implements >shouldComponentUpdate() with a shallow prop and state comparison. If you >are confident you want to write it by hand, you may compare this.props >with nextProps and this.state with nextState and return false to tell >React the update can be skipped.
You can find some documentation about the react render process on the following pages: https://facebook.github.io/react/docs/reconciliation.html https://facebook.github.io/react/docs/optimizing-performance.html
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