Redux dispatch causing component local state to reset

狂风中的少年 提交于 2020-01-03 02:08:47

问题


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).

  1. Is this the correct behavior for my component? Should the component's state reset when it mounts a second time (re-render)?
  2. If (1) is true, is there a way to preserve certain variable values in this.state when 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

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