2017-02-16 132 views

回答

2
shouldComponentUpdate(nextProps, nextState) { 
    if ('not rerneder condition') { 
    return false; 
    } 
} 
+1

'else {return true}' – jiyinyiyong

0

将shouldComponentUpdate函数添加到该组件并返回false。

shouldComponentUpdate() { 
    return false; 
} 
0

为了忽略对组件的更新,您应该使用shouldComponentUpdate生命周期方法。这应该在组件类中实现并始终返回false。这里有一个例子:

class CustomComponent extends React.Component { 
    shouldComponentUpdate() { 
    return false; 
    } 

    render() { 
    /* ... */ 
    } 
} 

希望这有助于!

0

除了shouldComponentUpdate,你可以尝试使用组件的属性key停止重新呈现。如果预览key与下一个key相同,则React不会重新渲染此组件。

+0

你能举一些例子吗? – TechTurtle

+0

'键应该是稳定的,可预测的和独特的。不稳定的键(如Math.random()生成的那些键)将导致许多组件实例和DOM节点被不必要地重新创建,这可能会导致性能下降并丢失子组件中的状态。 github.io/react/docs/reconciliation.html#tradeoffs)。在某些情况下,如多个嵌套组件,它可以工作! –