我一直在阅读React文档并遇到shouldComponentUpdate()。我的理解是,每次setState()调用时,都会重新渲染该组件。
shouldComponentUpdate()
setState()
我的问题是,如果要更新的值是 SAME 作为当前状态值,这会触发重新渲染事件吗?否则我将不得不手动检查当前值和要更新的值shouldComponentUpdate()
官方的React文档指出:
默认行为是在每次状态更改时重新呈现…
https://reactjs.org/docs/react- component.html#shouldcomponentupdate
这意味着默认情况下,render()如果组件state或props值的任何更改将执行。
render()
state
props
您可以使用shouldComponentUpdate()覆盖此默认行为。这是一个仅在状态值更改时更新的示例。
shouldComponentUpdate(nextProps, nextState) { return this.state.someValue !== nextState.someValue; }
注意:此示例完全忽略props。因此,对的任何更改props都不会触发render()。