因此,假设我有以下字符串数组:
const types = [
"type1",
"type2",
"type3" ]然后我还有一个像这样的useEffect钩子:
useEffect( () => {
if (someOtherString in types) { doSomething...}
}, [someOtherString])上面的useEffect钩子的问题是,它会要求添加对“类型”变量的依赖。因此,如果我将其添加到依赖项列表中:
,[someOtherString, types])
它将重新渲染大约3到4次。
如何创建可以读取数组或字典的useEffect,而不将其添加到依赖项中?
发布于 2020-09-02 18:51:39
如果将条件本身作为依赖项呢?示例:
const shouldDoSomething = someOtherString in types;
useEffect(() => {
if (shouldDoSomething) { /** Do something ! */ }
}, [shouldDoSomething])https://stackoverflow.com/questions/63703772
复制相似问题