问题
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:
- React Router doesn't store state between page loads. It just routes traffic between components and handles history, among other things.
- 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