角色管理前端
需求:只要执行 setState(), 就会调用 render 从新渲染。因为有时调用了 setState,可是并无发生状态的改变,以至于没必要要的刷新react
默认返回 true,始终在一进行 setStete 时就进行渲染,即便数据无变化面试
将新值 与 旧值 进行比较,改变了则进行渲染数组
优化: import {PureComponent} from "react" // 原理也是重写 shouldComponent 将组件状态/属性数据进行改变的判断性能优化
将类 继承于 PureComponent 性能
可是这样也带来了新问题: 在对 state 中数组进行 push 修改时优化
缘由: state 中存的是对 对象的引用变量,因为未发生改变,因此不会刷新页面this
解决: roles = [...this.state.roles, role] // 从新生成新的对象,roles 指向新的对象spa
总结:对象
1. 使用 PureComponent 代替 Component
2. 从 state 中取出数组或者对象,尽可能
const todos = [...this.state.todos]; // ES6
或者 const todos = this.state.todos.splice(); // ES5
5
5
5
5
5
5
5
5
4
5
5
5
5
5
55
5
5
5
5
5
5
5
5
5
5