我一直在使用react.js創建一個小應用程序。我過分考慮「表現」。如何讓道具不可變以防止在React中重投?
所以我有一個簡單的子組件名爲「微調」。我的目標是確保這個組件不會重新渲染。
這裏是我的組件:
import React, {PureComponent} from 'react';
export default class Spinner extends PureComponent {
render() {
return (
<div className="spinner">
<div className="bounce1"></div>
<div className="bounce2"></div>
<div className="bounce3"></div>
</div>
)
}
}
在重新渲染與時間「反應 - 插件-PERF」,組分總是渲染,我使用PureComponent因爲我想的是組件呈現只有一次,我讀到我可以使用不可改變的道具,但我不知道如何讓這成爲可能。
如果我做一些像這樣:
componentDidMount() {
this.renderState = false;
}
shouldComponentUpdate(nextProps, nextState) {
return (this.renderState === undefined) ? true : this.renderState;
}
它渲染只有一次,但我相信有一個更好的辦法。
如何避免重新渲染?或者也許我怎麼能製作一個不可改變的道具?
現在看起來更好 – FurkanO