简体   繁体   中英

Does react re-render components that don't depend on state when the state changes?

The StateIndependentComponent is a heavy component and StateDependentComponent is light but the state changes multiple times and quickly.

So, will StateIndependentComponent be re-rendered every time the state changes?

<StateIndependentComponenet />
<StateDependentComponent data={this.state.data} />

It depends how you implemented your components. By default yes, but you can use React.PureComponent (class components) or React.memo (function components) to make the component only rerender when it's props or state actually change.

class StateIndependentComponenet extends React.PureComponent {
    ...
}

or

const StateIndependentComponenet = React.memo((props) => {
    ...
})

Be sure to care about the note that is in the React docs linked above, because your component will not rerender seemingly randomly if you are mutating state by accident.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM