The parent control only updates the state, causing the child control to trigger the 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

props is not set when loading Sub1 in Parent.
is it reasonable that the state in Parent changes when the button is clicked, causing the componentWillReceiveProps event to be triggered in Sub1?

Mar.09,2021

Edit 5v5x97lo7l
this is true because the state of the parent component has changed and the parent component has been updated, but the child component has been updated without referencing any properties, which is indeed not very reasonable, although there will not be any changes after the final dom diff

Menu