角色管理前端
需求:只要執行 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