Both the react- master component and the parent component should control the explicit and recessive of the grandson component.

for example, both the master component and the parent component have to control the explicit and recessive of the grandson component, how to control it?

Oct.25,2021

it is recommended to write as a level component, so that only one prop, is easier to control.
if you have to write this,
1, define the prop of the grandchild component in the outermost layer (master component) (the default false), is passed to the parent component, determine whether the prop has changed in the componentWillReceiveProps hook of the parent component, and modify the state, of the parent component to pass to the grandchild component according to this change.
2. The control of the parent component has nothing to do with the outermost component, just modify the state directly.

A better way is to control a prop, with redux or mobx,.


go straight to the code:

class GF {
    state = {visibel: false}
    render() {
        return (
        <>
            <Parent visible={this.state.visible}/>
            <Child visible={this.state.visible}/>
        </>
       );
    }
}

class Parent {
    render() {
        return <Child visible={this.props.visible}/>
    }
}

class Child {
    render() {
        return <div visible={this.props.visible}></div>
    }
}

make up the rest by yourself.

Menu