ユースケースは、特定の prop 値をすべての子孫コンポーネントに「楽に」渡したい場合です。これがReactで可能かどうかはわかりません。
したがって、これを行う代わりに:
class Parent extends Component {
constructor() {
super(props);
this.props.componentID = "123456";
}
render() {
return <Child1 componentID={this.props.componentID} />
}
}
class Child1 extends Component {
render() {
return <Child2 componentID={this.props.componentID} />
}
}
class Child2 extends Component {
render() {
return <div>{this.props.componentID}</div>
}
}
次のようにします。
class Parent extends Component {
constructor() {
this.props.componentID = "123456";
}
passComponentIDToAllDescendantComponents() {
// Some super nifty code
}
render() {
return <Child1 />
}
}
// etc...
助けてくれてありがとう