我有一个 Canvas 组件,大致如下所示:
class Canvas extends React.Component{
saveRef = node => {
this._canvas = node;
}
shouldComponentUpdate(){
/*I will never re-render this component*/
return false;
}
componentWillReceiveProps( nextProps ){
/*Here I do manipulations with this._ctx, when new props come*/
}
render(){
return (
<canvas ref={this.saveRef} />
);
}
componentDidMount(){
this._ctx = this._canvas.getContext( "2d" );
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
React 社区开始弃用componentWillReceiveProps
,以将其替换为getDerivedStateFromProps
. 我可以componentDidUpdate
用来执行我的绘图,但随后我需要删除shouldComponentUpdate
,我将有很多无用的渲染调用。当新props出现时,在 react 16.3 中更新我的组件的正确性能方法是什么?