在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問(wèn)答/HTML/ 父控件僅更新state導(dǎo)致子控件觸發(fā)componentWillReceivePr

父控件僅更新state導(dǎo)致子控件觸發(fā)componentWillReceiveProps

//Parent.js
class Parent extends React.Component {
    constructor() {
        super();
        this.state = {
            name: 'zhangsan'
        }
    }

    test() {
        this.setState({
            name: 'lisi'
        })
    }

    render() {
        return (
            <div>
                <button onClick={this.test.bind(this)}>gogo</button>                
                <Sub1 />
            </div>
        )
    }
}

export default Parent
//Sub1.js
class Sub1 extends React.Component {
    componentWillReceiveProps(nextProps) {        
        console.log('componentWillReceiveProps from sub1')
    }

    render() {
        return <div>Sub1x</div>
    }
}

export default Sub1

Parent中加載Sub1時(shí)未設(shè)置props。
點(diǎn)擊按鈕時(shí),Parent中的state發(fā)生了變化,導(dǎo)致了Sub1中觸發(fā)了componentWillReceiveProps事件,這個(gè)合理嗎??

回答
編輯回答
心悲涼

Edit 5v5x97lo7l
看了下確實(shí)是這樣的,是因?yàn)楦附M件的state變化,父組件有更新,但是子組件沒(méi)有沒(méi)有引用任何屬性的情況下也更新了,確實(shí)不是很合理,雖然最后dom diff之后不會(huì)有任何變化

2017年12月8日 19:08