How do I clear location.state in react-router on page reload?

前端 未结 4 1880
余生分开走
余生分开走 2021-01-03 23:01

I am currently passing my state on route change like below:



        
4条回答
  •  轮回少年
    2021-01-03 23:18

    I also ran into this problem, and what I ended up doing was retrieving the browser history from react router and clearing specific location.state properties. So in your case, it would be transaction. I did this in componentDidMount so that after the first time you go to the page, that property should no longer be there,

    import createHistory from 'history/createBrowserHistory'
    
    ...
    
    componentDidMount(){
        const history = createHistory();
        if (history.location.state && history.location.state.transaction) {
            let state = { ...history.location.state };
            delete state.transaction;
            history.replace({ ...history.location, state });
        }
    }
    

提交回复
热议问题