Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 在React中单击后退按钮时触发事件操作_Reactjs_Back Button_Browser History - Fatal编程技术网

Reactjs 在React中单击后退按钮时触发事件操作

Reactjs 在React中单击后退按钮时触发事件操作,reactjs,back-button,browser-history,Reactjs,Back Button,Browser History,我有以下组件 class NewCampaign extends Component { constructor(props) { super(props) this.nextStep = this.nextStep.bind(this) this.previousStep = this.previousStep.bind(this) this.state = { page: 1, } } nextStep() {

我有以下组件

class NewCampaign extends Component {
  constructor(props) {
    super(props)
    this.nextStep = this.nextStep.bind(this)
    this.previousStep = this.previousStep.bind(this)

    this.state = {
      page: 1,
    }
  }  

  nextStep() {
      var newStep = this.state.step +1;
       this.setState({ page: newStep })
  }

  previousStep() {  
      var newStep = this.state.step -1;
      this.setState({ page: newStep })
  }


  render() {
    return (
        <div className="campaign">
          {page === 1 && <Step1Page nextStep={this.nextStep}/>}
          {page === 2 && <Step2Page previousStep={this.previousStep} nextStep={this.nextStep}/>}
          {page === 5 && <Step5Page previousStep={this.previousStep} />}                              
      </div>

    )
  }
}
类NewCampaign扩展组件{
建造师(道具){
超级(道具)
this.nextStep=this.nextStep.bind(this)
this.previousStep=this.previousStep.bind(this)
此.state={
页码:1,
}
}  
下一步(){
var newStep=this.state.step+1;
this.setState({page:newStep})
}
上一步(){
var newStep=this.state.step-1;
this.setState({page:newStep})
}
render(){
返回(
{page==1&&}
{page==2&&}
{page==5&&}
)
}
}
目前,每个组件步骤页面有2个按钮:一个prev和一个next按钮,单击该按钮将更新称为page的状态

这个很好用。但是,现在请求启用浏览器上的
后退按钮
,以转到上一步

我不确定是否可以使用
浏览器历史记录
,因为我仍然在同一条路线上。唯一正在改变的是名为
page
的组件状态,它由nextStep和PreviousStep两个操作触发

是否有办法确定何时单击后退按钮并激活上一步操作