How to tell useEffect to track substate
See original GitHub issueimport React, { useEffect } from "react";
import { useState } from "@hookstate/core";
const Component = () => {
const state = useState({ foo: 1, bar: 2 });
// will trigger when state.bar changes, which is wrong
useEffect(() => {
console.log(state.foo.get());
}, [state.foo]);
// will trigger only when state.foo changes, which is good
useEffect(() => {
console.log(state.foo.get());
}, [state.foo.value]); // but eslint complains state.foo is a dependency that was not listed here
return (
<>
<h1>Hello dude {state.bar.get()}</h1>
<button onClick={() => state.bar.set(p => p+1)}>
Update state.bar
</button>
<button onClick={() => state.foo.set(p => p+1)}>
Update state.foo
</button>
</>
);
};
export default Component;
I have this code, leaving just state.foo in my dep array will make the effect trigger always, but it I put state.foo.value in dep array it will work, but eslint complains about state.foo being a dependency not listed, I can disable the warning with // eslint-disable-next-line react-hooks/exhaustive-deps though
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
How to track individual state in React.useEffect()?
Just use separate useEffects for each purpose: React.useEffect(() => { // code only run when firebase state change addNotify(); ...
Read more >Using the Effect Hook - React
We declare the count state variable, and then we tell React we need to use an effect. We pass a function to the...
Read more >How the useEffect Hook Works (with Examples) - Dave Ceddia
The useEffect hook is the Swiss Army knife of all the hooks. It's the solution to many problems: how to fetch data when...
Read more >The last guide to the useEffect Hook you'll ever need
Understanding how the useEffect Hook works, and why it requires a wholesale shift in mindset, is essential to writing modern React code.
Read more >How to Listen for State changes in React | bobbyhadz
Use the useEffect hook to listen for state changes in React. You can add the state variables you want to track to the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

cool, what I needed.
Hi,
Yes, I am aware about this. Most of the time I handled it by ignoring the eslint warning. It is OK, because values returned by hookstate state.get() are always the latest no matter when it was captured in an effect or somewhere else.
However, in your particular case, I think the eslint warning will go away if you do this:
console.log(state.foo.value);- usevalueinstead ofgetinside of the useEffect callback. I have not checked it. Please let me know.