我正在尝试通过 redux-thunk 操作查询我的 Firebase 后端,但是,当我在使用 的初始渲染中这样做时useEffect(),我最终遇到此错误:
Error: Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops.
Run Code Online (Sandbox Code Playgroud)
我的操作只是返回一个 Firebase 查询快照,然后我在我的减速器中收到了它。我使用钩子来调度我的动作:
export const useAnswersState = () => {
return {
answers: useSelector(state => selectAnswers(state)),
isAnswersLoading: useSelector(state => selectAnswersLoading(state))
}
}
export const useAnswersDispatch = () => {
const dispatch = useDispatch()
return {
// getAnswersData is a redux-thunk action that returns …Run Code Online (Sandbox Code Playgroud) 在react-redux v7中,我们现在有了useDispatch()钩子来获取对商店调度的引用。看到这里:https : //react-redux.js.org/api/hooks#usedispatch
将所得dispatch然后功能需要被列为任何依赖关系useEffect,useMemo等钩。为什么是这样?
经过一些测试,该应用程序仍然可以完美运行而不会dispatch被列为依赖项,因此我只能假定它没有变化。
在任何情况下dispatch功能都可能改变吗?