简体   繁体   English

useEffect 具有详尽的 deps 循环

[英]useEffect with exhaustive deps loop

When modal.error sets to true , then my effect should be run:modal.error设置为true ,应该运行我的效果:

useEffect(() => {
  if (modal.error) {
    setModal({...modal, prompt: false})
  }
}, [modal]);

modal - is an object: modal - 是一个对象:

const [modal, setModal] = useState<SomeInteface>({
  success: false,
  error: false,
  prompt: false
})

However i want in useEffect deps only modal.error , but when i do so, linter warns me about exhaustive/deps annoying rule, why should i put my entire object in to a deps array, instead of just one of its properties.但是,我只希望在useEffect deps 中使用modal.error ,但是当我这样做时,linter 会警告我有关详尽/ deps 烦人的规则,为什么我要将整个对象放入 deps 数组,而不仅仅是它的一个属性。 It's obviously going for infinite loops, because object modal is always going to change.这显然是无限循环,因为对象modal总是会改变。

I still don't get, why did React team went away from lifecycle's paradigm which was so convenient instead of this 3 in one thing, which is more confusing with these exhaustive/deps rules, i've read a lot of articles about useEffect , but still, i think lifecycles is much better.我仍然不明白,为什么 React 团队放弃了如此方便的生命周期范式,而不是这useEffect ,这对这些详尽的/deps 规则更令人困惑,我已经阅读了很多关于useEffect的文章,但是,我仍然认为生命周期要好得多。

UPDATE 1更新 1

const removePartner = (): void => {
  setModal({
    ...modal,
    prompt: false
  });
  deleteRequest(undefined, modal.id).then((resp: any) => {
    if (resp) {
      setModal({ ...modal, success: true });
    } else {
      setModal({ ...modal, error: true });
    }
  });
};

UPDATE 2更新 2

TSX part:多伦多证券交易所部分:

<Swal
  show={modal.prompt}
  icon={Icons.warning}
  title="Warning!"
  text="Are you sure?"
  cancelButton={true}
  onModalClose={() => setModal({ ...modal, prompt: false })}
  onAccept={removePartner}
/>
<Swal
  show={modal.error}
  icon={Icons.error}
  title="Error"
  text={fetchError!}
  onModalClose={() => setModal({ ...modal, error: false })}
/>
<Swal
  show={modal.success}
  icon={Icons.success}
  title="Success!"
  text="You have removed a user!"
  onModalClose={() => {
    setModal({ ...modal, success: false });
    doFetch().then();
  }}
/>

I think you don't need an useEffect.我认为你不需要 useEffect。 You just need to bind your state to the modal.您只需要将您的状态绑定到模态。

<Modal open={modal.error} />

You just need a condition to set the state of modal.error你只需要一个条件来设置 modal.error 的状态

I changed my mind about using objects in useState hooks, they are meant for a primitive values, that makes sense, otherwise i recommended others to use useReducer for complex objects.我改变了在useState钩子中使用对象的useState ,它们用于原始值,这是有道理的,否则我建议其他人将useReducer用于复杂对象。 Then you won't have issues with useEffect deps loops cause of the whole object changes.那么你就不会useEffect deps 循环导致整个对象更改的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ESLint 希望 setSate 作为 useEffect 的依赖,但这会导致无限循环(react-hooks/exhaustive-deps) - ESLint wants setSate as a dependency for useEffect but this causes an infinite loop (react-hooks/exhaustive-deps) 使用 eslint Exclusive-deps 响应订阅/取消订阅的 useEffect 依赖项 - React useEffect dependencies for subscribe/unsubscribe with eslint exhaustive-deps React hooks - useEffect exhaustive deps - location.hash 的循环依赖 - React hooks - useEffect exhaustive deps - cyclic dependency on location.hash useEffect 和 &#39;react-hooks/exhaustive-deps&#39; linting - useEffect and 'react-hooks/exhaustive-deps' linting 反应 useEffect 带有警告的钩子 react-hooks/exhaustive-deps - React useEffect hook with warning react-hooks/exhaustive-deps 我如何正确使用 useEffect 进行异步获取调用和反应? 反应钩子/详尽的deps - How do i properly use useEffect for a async fetch call with react? react-hooks/exhaustive-deps 如何解决`react-hooks/exhaustive-deps`的`React Hook useEffect缺少依赖`? - How to solve `React Hook useEffect has a missing dependency` of `react-hooks/exhaustive-deps`? Eslint React Hooks错误:eslint-plugin-react-hooks用尽详尽的警告警告useEffect中的功能依赖项 - Eslint React Hooks Error: eslint-plugin-react-hooks exhaustive deps warning for a function dependency in useEffect 将详尽的依赖项添加到useEffect和useCallback会导致无限循环 - Adding exhaustive dependencies into useEffect and useCallback causes infinite loop React Hook useEffect 缺少依赖项:&#39;formValues&#39;。 包括它或删除依赖数组 react-hooks/exhaustive-deps - React Hook useEffect has a missing dependency: 'formValues'. Either include it or remove the dependency array react-hooks/exhaustive-deps
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM