React/Redux:如何在存储更改后忽略重新渲染?
shouldComponentUpdate(nextProps, nextState) {
if ('not rerneder condition') {
return false;
}
}
将shouldComponentUpdate函数添加到该组件并返回false。
shouldComponentUpdate() {
return false;
}
为了忽略对组件的更新,您应该使用shouldComponentUpdate生命周期方法。这应该在组件类中实现并始终返回false。这里有一个例子:
class CustomComponent extends React.Component {
shouldComponentUpdate() {
return false;
}
render() {
/* ... */
}
}
希望这有助于!
除了shouldComponentUpdate
,你可以尝试使用组件的属性key
停止重新呈现。如果预览key
与下一个key
相同,则React不会重新渲染此组件。
你能举一些例子吗? – TechTurtle
'键应该是稳定的,可预测的和独特的。不稳定的键(如Math.random()生成的那些键)将导致许多组件实例和DOM节点被不必要地重新创建,这可能会导致性能下降并丢失子组件中的状态。 github.io/react/docs/reconciliation.html#tradeoffs)。在某些情况下,如多个嵌套组件,它可以工作! –
'else {return true}' – jiyinyiyong