本文正在参加「金石计划」
-
使用生产版本
Create React App
npm run buildwebpack
module.exports = merge(common, { mode: "production", }); -
虚拟化长列表
-
shouldComponentUpdate和 React.PureComponentshouldComponentUpdate中返回false来跳过整个渲染过程。其包括该组件的render调用以及之后的操作class CounterButton extends React.Component { constructor(props) { super(props); this.state = {count: 1}; } shouldComponentUpdate(nextProps, nextState) { if (this.props.color !== nextProps.color) { return true; } if (this.state.count !== nextState.count) { return true; } return false; } render() { return ( <button color={this.props.color} onClick={() => this.setState(state => ({count: state.count + 1}))}> Count: {this.state.count} </button> ); } }检查
props和state中所有的字段浅比较,使用React.PureComponent -
不可变数据
concat
this.setState(state => ({ words: [...state.words, 'marklar'], }));function updateColorMap(colormap) { return {...colormap, right: 'blue'}; }function updateColorMap(colormap) { return Object.assign({}, colormap, {right: 'blue'}); }