鍍金池/ 問答/網(wǎng)絡(luò)安全  HTML/ 關(guān)于react shouldComponentUpdate 什么時(shí)候重新rend

關(guān)于react shouldComponentUpdate 什么時(shí)候重新render的問題

背景:
將cytoscape(關(guān)系圖插件)單獨(dú)放在一個(gè)文件中,如下:

export default class Cytoscape extends React.Component {
    componentDidMount() {
       
    }    
    shouldComponentUpdate(nextProps) {
        let prevState = this.props;
        let nextState = nextProps;
        console.log("nextProps:",Immutable.fromJS(prevState.data).equals(Immutable.fromJS(nextProps.data)));
        if (!Immutable.fromJS(prevState.data).equals(Immutable.fromJS(nextProps.data))) {
            return true
        }else{
            return false
        }
    }
    render() {
        ...
        }

在shouldComponentUpdate方法中,判斷關(guān)系圖的數(shù)據(jù)是否改變,改變了就從新渲染,不改變就不重新渲染。
問題:
因?yàn)檫@個(gè)關(guān)系圖上的節(jié)點(diǎn)用戶可以隨便拖動(dòng)位置,現(xiàn)在在右上角加一個(gè) 還原 的按鈕,但是點(diǎn)擊這個(gè)還原按鈕,data也沒有改變...導(dǎo)致關(guān)系圖并沒有重新渲染達(dá)到還原的效果...

回答
編輯回答
逗婦惱

是不是可以點(diǎn)擊還原按鈕的時(shí)候,改變一下state,在shouldComponentUpdate里面不止做props變化的檢測(cè),也考慮state的變化。

shouldComponentUpdate(nextProps, nextState)
2017年6月12日 06:54