React Router + back button is resetting my app state

人盡茶涼 提交于 2021-02-11 06:55:27

问题


I'm using React Router Dom and the BrowserRouter object to enable browser based navigation for a site I'm building. Navigation works well, but if a user hits the back button, the entirety of my App's state is wiped clean.

I'm not sure why this is happening. I've tried searching around, but all of the answers I've found don't mention why App state is getting reset.

Here's my implementation:

<BrowserRouter>
    <Switch>
        <Route path="/" exact render={()=><LandingPage/>}
        <Route path="/about" render={()=><About/>}
    </Switch>
</BrowserRouter>

Am I doing something glaringly wrong? Could anyone with experience working with React Router give me some advice? Thanks so much!


回答1:


If someone like me has this problem in the future and comes here from Google, here's what's going on:

  1. React Router doesn't store state between page loads. It just routes traffic between components and handles history, among other things.
  2. You need a separate state management store to preserve state changes. (Redux was recommended).

Thanks @charlietfl for the info.



来源:https://stackoverflow.com/questions/52210499/react-router-back-button-is-resetting-my-app-state

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!