0
我在根據用戶的選擇構建一個組件。我已經成功完成了它,但是在嘗試實現返回按鈕時遇到了一些問題。在reactjs中轉到之前的狀態
我的代碼如下所示。
class ReportMainCat extends Component {
constructor(props) {
super(props);
this.state = {
postType: null,
}
this.changeType = this.changeType.bind(this);
this.report_next = this.report_next.bind(this);
};
report_next() {
if (this.state.postType == null) {
return <ReportFirst changeType={this.changeType}/>
}
else if (this.state.postType === 'sexual') {
return <ReportXContent changeType={this.changeType}/>
} else if (this.state.postType === 'selfharm') {
return <ReportThreatContent changeType={this.changeType}/>
}
}
changeType = (postType) => {
this.setState({postType})
this.setState({
showMainReportCats: false,
})
}
render() {
return (
<div className="top_of_overlay">
<div className="section_container text_align_center padding_10px">
<a className="">Report Category</a>
{this.report_next()}
</div>
</div>
)
}
}
我按如下方式綁定postType
值。
class ReportXContent extends Component {
constructor(props) {
super(props);
this.state = {
postType: '',
}
};
textType(postType) {
this.props.changeType(postType);
}
render() {
return (
<div className="text_align_left">
<div>
<div className="width_100 margin_bottom10px">
<input type="checkbox" ref="nudity" onClick={this.textType.bind(this,'nudity')}/>
<a>Nudity or Pornography</a>
</div>
<div className="width_100 margin_bottom10px">
<input type="checkbox" ref="minor" onClick={this.textType.bind(this,'minor')}/>
<a>Includes Minors</a>
</div>
</div>
<ReportButtons/>
</div>
)
}
}
我後退按鈕
<div>
<button className="float_right margin_left5px" onClick={this.props.back_report}>Back</button>
</div>
所以基本上我想要做的就是這個。例如:
例如:如果用戶選擇postType
作爲sexual
它將返回ReportXContent
組件。當用戶點擊後退按鈕時,如何返回到第一頁。
謝謝。
是烏拉圭回合的答案是真棒..一個問題,但..保存lastPostType時,我只能保存一個狀態。所以我可以回去只有一步。我該如何解決它? – CraZyDroiD
增加了另一個詳細說明方法的例子:) –
非常感謝你 – CraZyDroiD