牛魔王的故事
2022-05-14 14:42:09
我有 2 個組件 OptinPage(父)和 TermsOfServices(子)。Optin Page 僅用于渲染 TermsOfServices 組件,可以在應用程序的其他地方重用。我想在我的 OptinPage 組件中使用我的 TermsOfServices 組件的狀態來顯示條件頁面的數量(1/2、2/2)。如果沒有 Redux,我怎么能做到這一點?
2 回答

動漫人物
TA貢獻1815條經驗 獲得超10個贊
請嘗試這種方法
class OptIn extends Component{
state = {}
receiveChildState = (childState) => {
console.log(childState)
}
render(){
return(
<TermsOfService receiveChildState={this.receiveChildState}/>
);
}
}
class TermsOfService extends Component{
state = {
x: 1
}
updateState = () => {
const {x} = this.state;
const {receiveChildState} = this.props;
this.setState({x: x+1});
receiveChildState(x+1);
}
render(){
return(
<button onClick={this.updateState}> Terms of service </button>
);
}
}
添加回答
舉報
0/150
提交
取消