【问题标题】:React hook - `useEffect` with clean upReact hook - 带有清理的`useEffect`
【发布时间】:2020-08-03 16:01:35
【问题描述】:

我正在学习 React Hook 和 useEffect,但我可能没有掌握正确使用它的关键点。

我有以下场景:

  1. 如果用户切换下拉菜单,如果当前页面有任何更新,将检查是否有来自全局状态的 isAnyChange 标志。
  2. 如果没有变化,将继续并重定向到下一页,
  3. 如果有任何更改,将显示确认对话框。

然后在确认对话框中,将有 2 个按钮:

  • 4.1 要求保存、检查验证和重定向,
  • 4.2 一请求不保存和重定向。

我使用不同的useEffect 包含了上述逻辑,但我不确定我是否正在做正确的方式,因为它看起来与我自己有关并且使事情变得复杂,什么是分解我的功能的最佳方式然后?我是否在这里通过使用那些local state flag 来触发不同的useEffect 进行正确的设计?不过我想不出更好的办法...

const pageComponent = props => {
   const [isSelectChange, setIsSelectChange] = useState(false);
   const [canRedirect, setCanRedirect] = useState(false);
   const [isSaveCompleted, setIsSaveCompleted] = useState(false);

   const anyChange = useSelector(state => return state.anyChange;);
   const anyValidError = useSelector(state => return state.anyValidError;);

   // step 1. dropdown change to change local state and trigger useEffect in step 2
   const selectChange = () => {
      setIsSelectChange(true);
   }

   // step 2. state isSelectChange to trigger step 3 howConfirmDialog or useEffect in step 6
   useEffect(() => {
     if (isSelectChange) {
        if (anyChange) { showConfirmDialog(); }
        else { setCanRedirect(); }
        // should I placed it here? but this seems to trigger the same useEffect a second time even though no code is executing 
        // setIsSelectChange(false);
     }

     return () {
        // clean up to reset the flag back to avoid looping here on isSelectChange, is it the right place to do it here?
        setIsSelectChange(false);
     }
   }, [isSelectChange])

   // step 3. dispatch a confirm dialog
   const showConfirmDialog() => {
      dispatch(openConfirmDialog(
        dialog: <ConfirmDialog confirmhdlr={saveAndRedirect} cancelhdlr={cancelAndRedirect} />
      );
   }

   // step 4.1 save and redirect
   const saveAndRedirect = async () => {
      await(savePage());
      // this is to trigger another useEffect monitoring on isSaveCompleted flag
      setIsSaveCompleted(true);
      setCanRedirect(true);
   }
   
   // step 4.2 cancel and redirect
   const cancelAndRedirect = () => {
      setCanRedirect(true);
   }
   
   // step 5. state isSaveCompleted to trigger
   useEffect(() => {
     if (isSaveCompleted && !anyValidError) {
        // this is to trigger another useEffect monitoring on "canRedirect"
        setCanRedirect(true);
        // should I placed it here? but this seems to trigger the same useEffect a second time even though no code is executing 
        // setIsSaveCompleted(false);
     }

     return () {
        // clean up to reset the flag back to avoid looping here on isSaveCompleted flag, is it the right place to do it here?
        setIsSaveCompleted(false);
     }
   }, [isSaveCompleted])

   // step 6. state canRedirect to trigger redirect
   useEffect(() => {
     if (canRedirect) {
       if (isSaveCompleted && !anyValidError) {
          // this is to trigger another useEffect monitoring on "canRedirect"
          setCanRedirect(true);
       }
       // should I placed it here? but this seems to trigger the same useEffect a second time even though no code is executing 
       // setCanRedirect(false);
     }

     return () {
        // clean up to reset the flag back to avoid looping here on canRedirect flag, is it the right place to do it here?
        setCanRedirect(false);
     }
   }, [canRedirect])

   return (<select onChange={selectChange}>)
}

如你所见,我有不同的标志来触发不同的useEffect,我可以在一个函数中完成所有这些,那么为什么要在这里使用useEffect?

【问题讨论】:

  • 您不需要在清理函数中重置状态 - 当组件卸载时调用它们,在这种情况下所有状态也会被清除。返回函数用于取消订阅等
  • @thedude,谢谢!那么我应该将重置状态放在 useEffect 执行中但在 return() 清理之外吗?似乎重置会再次调用useEffect,我不确定我在这里是否正确......

标签: javascript reactjs react-hooks


【解决方案1】:

这不是一个答案,但它应该可以帮助您更好地理解钩子。你有三个 useEffects 并且每一个在任何状态发生变化时都会起作用。所以你的第 1 步会触发下面的所有 useEffects。像这样使用依赖列表:

useEffect(()=>{
  ...
}, [isSelectChange]) // this hook fires only when isSelectChange state changes.

useEffect(()=>{
  ...
}, []) // this hook fires only once, when the component ie rendering for the very first time

【讨论】:

  • 谢谢,我知道建议使用依赖列表,这里我想了解更多关于使用useEffect的机制,如您所见,我有不同的标志来触发不同的useEffect,我可以在一个函数中完成所有这些,那为什么要在这里使用useEffect?
  • 同样的问题 - 我再次检查了您的代码,但没有发现您有不同的触发器来使用 useEffects。它们三个都没有依赖列表,这意味着它们将以相同的方式工作——在任何状态发生变化之后。您在钩子内检查条件的事实并没有设置依赖关系。
  • 嗨@Tarukami,我已经将我的依赖添加到useEffect,但是,关于我最初的困惑,我在这里做正确的设计是通过使用那些local state flag 来触发不同的useEffect ?不过我想不出更好的办法……
  • 如果我将我的标志作为依赖项添加到 useEffect 中,每当发生变化时,即使我的标志没有改变,我的所有 3 个 useEffect 仍然会触发......
猜你喜欢
  • 2022-01-11
  • 2019-10-08
  • 2021-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-07
  • 2019-12-28
相关资源
最近更新 更多