question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

React Hook useEffect has a missing dependency

See original GitHub issue
import { useState } from "@hookstate/core";
import { useEffect } from "react";

export default function App() {
  const state = useState("whatever");

  useEffect(() => {
    state.set("foobar");
  }, []);

  return (<></>);
}

Linter warning: React Hook useEffect has a missing dependency: 'state'. Either include it or remove the dependency array. (react-hooks/exhaustive-deps)

The reason I’m updating the state in the effect is because I’m fetching data and updating the state when it comes back. I just didn’t include that in the code above.

I don’t want to pass in state into the array because I don’t want it to re-render every time the state changes. Is there a way to do this without disabling the linting rule?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
avkonstcommented, Jun 10, 2021

It is React thing. You can ask the same question about any other variable, not necessary state variable. As far as I know you can disable a specific lint rule per line. You can also try the following:

import { useState } from "@hookstate/core";
import { useEffect } from "react";

export default function App() {
  const state = useState("whatever");
  const stateSetter = React.useMemo(state.set);

  useEffect(() => {
    stateSetter.current("foobar");
  }, [stateSetter]);

  return (<></>);
}
0reactions
avkonstcommented, May 28, 2022

“Is there a way to do this without disabling the linting rule?” as @TroyJoachim suggested, set the state when it is fetched. You can also check if hookstate’s built in support for async state would help in your case.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to fix missing dependency warning when using useEffect ...
Resolution: Step 1: Move business logic it to separate const. Now the warning is: React Hook React.useEffect has a missing dependency: 'roleChecking'. const ......
Read more >
React Hook useEffect has a missing dependency error
The warning "React Hook useEffect has a missing dependency" occurs when the useEffect hook makes use of a variable or function that we...
Read more >
How To Fix "react hook useeffect has a missing dependency"?
Add the missing dependency; Move the dependency inside the useEffect hook; Disable the ESLint rule. This article will explore all of those ...
Read more >
How to fix - react hook useEffect has missing dependencies?
One of the least recommended solutions to fix “React Hook useEffect has a missing dependency” is to remove the dependencies and avoid the ......
Read more >
Solve - React Hook useEffect has a missing dependency error.
The warning “React Hook useEffect has a missing dependency” occurs when the useEffect hook makes use of a variable or function outside its...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found