[Eslint] React Hook useEffect 使用警告

31 阅读1分钟

问题

React Hook useEffect has a missing dependency: 'xxx'. Either include it or remove the dependency array  react-hooks/exhaustive-deps

解决

React Hook useEffect 缺少依赖项:“xxx”

  • 要么包括它

    useEffect(() => {
      setIsModalOpen(!modalopen);
    }, [modalopen]);
    
  • 要么删除依赖数组

    useEffect(() => {
      ...
    }, []);
    
  • 或者添加 Eslint 注释规避

    useEffect(() => {
      ...
      // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [modalopen]);