代码之家  ›  专栏  ›  技术社区  ›  Arash jahan bakhshan

redux未使用React 18重新呈现组件

  •  0
  • Arash jahan bakhshan  · 技术社区  · 2 年前

    我有一个定制的钩子,它处理基于用户身份验证的全局数据获取。

    const userState = useSelector(state => state.user.state)
    
    useEffect(() => {
        if(userState === "authenticated") doSomething()
        if(userState === "unauthenticated") doSomething()
        if(userState === "loading") doSomething()
    }, [userState])
    

    但由于我使用的是React 18 strict模式(在NextJS中),useEffect只在 userState === "loading" 值,并且在状态更新时不会运行。

    我不确定是否应该提供更多代码细节。如果你需要,告诉我,我会编辑这个问题。谢谢

    0 回复  |  直到 2 年前
        1
  •  0
  •   Moein Moeinnia    2 年前

    在React 18 with strict模式下,组件安装后,React立即模拟卸载和重新安装组件(仅在 开发环境 ) check react docs

    我想取消严格模式会解决你的问题。 为了获得一致的代码并防止不安全的生命周期发生,请使用 next lint 相反

        2
  •  0
  •   phry    2 年前

    React 18与React redux的版本存在一些问题<=7.2.7,因此请确保将其更新至7.2.8或新的react redux 8 beta(专门为react 18设计)