Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React, Does render on parent makes children re-render?

Tags:

reactjs

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?

  • Parent's render
  • -> child's shouldComponentUpdate gets called
  • -> if shouldComponentUpdate return true, child rerenders

Parent 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>
    )
  }
like image 946
eugene Avatar asked Oct 24 '16 12:10

eugene


People also ask

Do kids Rerender when parent Rerenders React?

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.

Does React re-render children?

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.

Do parents Rerender child changes?

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.

Does React update all children components once the state of a parent has changed?

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.


1 Answers

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

like image 118
Martin Bonetti Avatar answered Dec 02 '22 12:12

Martin Bonetti