Intercept/handle browser's back button in React-router?

前端 未结 12 1102
滥情空心
滥情空心 2020-11-27 03:47

I\'m using Material-ui\'s Tabs, which are controlled and I\'m using them for (React-router) Links like this:

    

        
12条回答
  •  鱼传尺愫
    2020-11-27 04:32

    here is how I ended up doing it:

    componentDidMount() {
        this._isMounted = true;
        window.onpopstate = ()=> {
          if(this._isMounted) {
            const { hash } = location;
            if(hash.indexOf('home')>-1 && this.state.value!==0)
              this.setState({value: 0})
            if(hash.indexOf('users')>-1 && this.state.value!==1)
              this.setState({value: 1})
            if(hash.indexOf('data')>-1 && this.state.value!==2)
              this.setState({value: 2})
          }
        }
      }
    

    thanks everybody for helping lol

提交回复
热议问题