【发布时间】:2021-09-19 21:59:23
【问题描述】:
我正在开发一个 nextjs 项目,其中我有一个与 pages 文件夹级别相同的 helpers 文件夹。
我在 helpers 文件夹中有一个 ts 文件,在这里我想根据最新状态获取最新状态和更新状态
这就是我获取状态的方式
store().getState()
store 从 store.js 导入的地方
我根据之前的状态更新状态
const state = store().getState()
if(!state.currentUser){ // here im checking if state has currentUser
store().dispatch(Action) // here im calling action which will update the state
}
do further operations
这里的问题是更新状态后我没有从store().getState() 获得更新的状态。我管理事情的方式是否正确?如何获取更新后的状态?
*EDIT* : Im sending a helper function as a prop to many if my page components. Now that i dont want to touch this , i somehow want to get the updated state and dispatch actions based on the state itself. Note that the hepler function is not a functional component
提前致谢
【问题讨论】:
标签: javascript reactjs redux react-redux next.js