简体   繁体   English

useEffect 详尽-deps 规则令人困惑

[英]useEffect exhaustive-deps rule is confusing

I have found a few questions related to this on stackoverflow, but nothing that satisfies a use-case which I have very regularly while using useEffect .我在 stackoverflow 上发现了一些与此相关的问题,但没有什么能满足我在使用useEffect时经常遇到的用例。 Let's say I have some code like this:假设我有一些这样的代码:

const ApiViewer = (props) => {
    const [result, setResult] = useState('')

    useEffect(async () => {
        setResult(await callApi(props.valueThatWillChangeButIOnlyCareAboutItsInitialValue))
    }, [])

    return <div>{result}</div>
} 

The exhaustive-deps rule will throw, asking me to put props.valueThatWillChangeButIOnlyCareAboutItsInitialValue in the dependency array. exhaustive-deps规则将抛出,要求我将props.valueThatWillChangeButIOnlyCareAboutItsInitialValue放入依赖数组中。 I don't want to do this as I only want the initial value.我不想这样做,因为我只想要初始值。 I may use that prop in a separate effect somewhere.我可能会在某处单独使用该道具。

I can't think of another way to write this either.我也想不出另一种写法。

I also have the problem that other people seem to have, ie using functions/etc that I know will never change.我也有其他人似乎有的问题,即使用我知道永远不会改变的功能/等。 I don't want to add them to this array, seems very dumb.我不想将它们添加到这个数组中,看起来很愚蠢。

Dan Abramov said that Usually disabling it is a mistake and will significantly bite you later . Dan Abramov 说,通常禁用它是一个错误,以后会严重咬你 I've been using it this way for years and I've seen no issues until this rule started popping up in a new project.我多年来一直以这种方式使用它,直到这条规则开始在新项目中出现之前,我没有发现任何问题。

Am I using React completely wrong?我使用 React 完全错误吗?

Edit: I've read through this thread more thoroughly, and have concluded that Dan Abramov really cares about programmers because he wants them to have a job for life .编辑:我已经更彻底地阅读了这个线程,并得出结论认为 Dan Abramov 真的很关心程序员,因为他希望他们有一份终生的工作 I am removing this rule from my projects now.我现在从我的项目中删除这条规则。

Don't worry, you are doing it right, Exhaustive dep is just a warning thrown by eslint.别担心,你做对了,Exhaustive dep 只是 eslint 抛出的一个警告。 and just because you got the warning it doesn't mean you are doing it completely wrong, If your code demands something to be run only on the initial load of the component.并且仅仅因为您收到警告并不意味着您做错了,如果您的代码要求仅在组件的初始加载时运行某些东西。 what you are doing it completely right.你在做什么是完全正确的。

Although I would suggest you don't remove this rule completely and to just suppress them where ever necessary.尽管我建议您不要完全删除此规则,而只是在必要时压制它们。

EDIT: The reason I said not to completely disable the rule was because, other hooks like useCallback and useMemo also works based on dependency list.编辑:我说不完全禁用该规则的原因是,其他钩子如useCallbackuseMemo也基于依赖项列表工作。 And if you failed to add your dependency list for those hooks, it can completely backfire on you.如果你没有为这些钩子添加你的依赖列表,它可能会完全适得其反。

Having the warning is always good, because you are not the only one who's gonna be working on a project, this can also help a new junior developer that you hire tomorrow.有警告总是好的,因为你不是唯一一个要在一个项目上工作的人,这也可以帮助你明天雇用的新初级开发人员。

If you want to ignore updates to a given prop then you can clarify that by using a useRef .如果您想忽略对给定道具的更新,那么您可以使用useRef来澄清这一点。 When referencing the ref inside a useEffect , you won't get any warnings because the base object returned from useRef never changes.useEffect中引用 ref 时,您不会收到任何警告,因为从 useRef 返回的基本useRef永远不会改变。 Using an initial prefix or something similar will make this obvious to the next developer as well.使用initial前缀或类似的东西也会让下一个开发人员明白这一点。

const ApiViewer = (props) => {
    const [result, setResult] = useState('')
    const initialValueThatWillChangeRef = useRef(props.valueThatWillChange);

    useEffect(async () => {
        setResult(await callApi(initialValueThatWillChangeRef.current))
    }, [])

    return <div>{result}</div>
} 

You can certainly construct examples where everything works as expected by ignoring the exhaustive deps rule, but it'll work until it doesn't.您当然可以通过忽略详尽的 deps 规则来构建一切都按预期工作的示例,但它会一直工作直到它不起作用。 If you convince yourself that the rule is not a rule but rather a suggestion, then you will eventually run into a nuanced case that breaks for reasons that are difficult to debug.如果您说服自己该规则不是规则而是建议,那么您最终会遇到一个微妙的案例,该案例会因难以调试的原因而中断。

Treating exhaustive deps as the true rule that it is will ensure components update as expected.将详尽的 deps 视为真正的规则,这将确保组件按预期更新。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM