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.

How to tell useEffect to track substate

See original GitHub issue
import 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:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
hp8wvvvgnj6asjm7commented, Apr 15, 2021

cool, what I needed.

1reaction
avkonstcommented, Nov 22, 2020

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); - use value instead of get inside of the useEffect callback. I have not checked it. Please let me know.

Read more comments on GitHub >

github_iconTop 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 >

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