2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

React Hook useEffect has a missing dependency. Either include it or remove the dependency array. のエラーを解消する

Posted at
React Hook useEffect has a missing dependency. 
Either include it or remove the dependency array.

React hooks のuseEffectを利用していると
上記のようなエラーがでて解決できたので記録のために残します。

##解決方法

// eslint-disable-next-line react-hooks/exhaustive-deps
をuseEffect内に追加することで解決できました。

useEffect(() => {
   // other code
   ...

   // eslint-disable-next-line react-hooks/exhaustive-deps
}, []) 

###実行する関数が、useEffect内のみで利用される場合

useEffect内で呼び出す関数が、useEffectの外にある場合、useEffect内で実行する必要があります。

#####変更前

  const setInitialUsers = () => {
      githubContext.setnIitialUser();
    };

  useEffect(() => {
    setInitialUsers();

    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);

#####変更後

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

###実行する関数が、useEffect外でも利用される場合

最初に紹介した
// eslint-disable-next-line react-hooks/exhaustive-deps
を追加して対応しましょう。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?