问题
I'm using Redux with React. I'm using this.state (component local state) to save component specific variables. Problem is that my components state resets to initial state whenever I dispatch an action (fetch action) and Store updates (mounts).
- Is this the correct behavior for my component? Should the component's state reset when it mounts a second time (re-render)?
- If (1) is true, is there a way to preserve certain variable values in
this.statewhen it receives new props from Redux store? I would prefer not to store all my components local states in Redux.
回答1:
Reason: I had implemented the react-router-dom Route component the wrong way. I was using the Router component function
<Router component={()=><Login / >}/>
which then would re-mount the child component.
Solution: Use the render function within the Router component.E.g.
<Router render={()=><Login / >}/>
来源:https://stackoverflow.com/questions/48990911/redux-dispatch-causing-component-local-state-to-reset