Reactjs 通过子组件显示隐藏组件
我在父组件中有两个步骤组件,其状态为:Reactjs 通过子组件显示隐藏组件,reactjs,react-router,Reactjs,React Router,我在父组件中有两个步骤组件,其状态为: state = { isStep1Active: true, isStep2Active: false, } 这是父级构造函数中设置的初始状态: this.state = { isStep1Active: true, isStep2Active: false, } 在构造函数本身中,我通过函数绑定了此状态,以便可以更改子组件中的状态: this.showStep2Func = this.
state = {
isStep1Active: true,
isStep2Active: false,
}
这是父级构造函数中设置的初始状态:
this.state = {
isStep1Active: true,
isStep2Active: false,
}
在构造函数本身中,我通过函数绑定了此状态,以便可以更改子组件中的状态:
this.showStep2Func = this.showStep2Func.bind(this);
this.hideStep1Func = this.hideStep1Func.bind(this);
showStep2Func() {
this.setState({
isStep2Active: true
});
}
hideStep1Func() {
this.setState({
isStep1Active: false
});
}
<div className="tab-content">
{
this.state.isStep1Active ?
<div className="tab-pane active" role="tabpanel" id="step1">
<Merchant1CreateAccountForm hideStep1={this.hideStep1Func} showStep2={this.showStep2Func} />
</div>
: null
}
{
this.state.isStep2Active ?
<div className="tab-pane" role="tabpanel" id="step2">
<Merchant2EmailVerificationForm/>
</div>
: null
}
</div>
此方法将发送到父组件中声明的子组件:
this.showStep2Func = this.showStep2Func.bind(this);
this.hideStep1Func = this.hideStep1Func.bind(this);
showStep2Func() {
this.setState({
isStep2Active: true
});
}
hideStep1Func() {
this.setState({
isStep1Active: false
});
}
<div className="tab-content">
{
this.state.isStep1Active ?
<div className="tab-pane active" role="tabpanel" id="step1">
<Merchant1CreateAccountForm hideStep1={this.hideStep1Func} showStep2={this.showStep2Func} />
</div>
: null
}
{
this.state.isStep2Active ?
<div className="tab-pane" role="tabpanel" id="step2">
<Merchant2EmailVerificationForm/>
</div>
: null
}
</div>
这是父组件中存在的两个组件的我的div:
this.showStep2Func = this.showStep2Func.bind(this);
this.hideStep1Func = this.hideStep1Func.bind(this);
showStep2Func() {
this.setState({
isStep2Active: true
});
}
hideStep1Func() {
this.setState({
isStep1Active: false
});
}
<div className="tab-content">
{
this.state.isStep1Active ?
<div className="tab-pane active" role="tabpanel" id="step1">
<Merchant1CreateAccountForm hideStep1={this.hideStep1Func} showStep2={this.showStep2Func} />
</div>
: null
}
{
this.state.isStep2Active ?
<div className="tab-pane" role="tabpanel" id="step2">
<Merchant2EmailVerificationForm/>
</div>
: null
}
</div>
如何重新渲染父组件?上面的代码不会重新呈现父组件隐藏和显示各自的组件。由于某些原因,这在npm启动后起作用。
不知道为什么它以前不起作用。由于某种原因,它在npm启动后起作用。 不知道为什么它以前不起作用