使用传播运算符添加依赖项时的useEffect警告

时间:2019-08-22 10:27:00

标签: reactjs eslint react-hooks eslint-plugin-react-hooks

将依赖项从数组传递到useEffect的第二个参数时,eslint会引发以下错误

// inputs need to go as part of effects dependency
export function useEffectAsync(effect, inputs) { 
     useEffect(() => {
         effect();
     }, [...inputs, effect]);
}

React Hook useEffect has a spread element in its dependency array. This means we can't statically verify whether you've passed the correct dependencies

用法:

useEffectAsync(async () => {
        await dispatch(getData());
        // set something once data is available 
     }
 }, [dispatch]);

0 个答案:

没有答案