Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getData dependency inside useEffect #132

Open
spalt08 opened this issue Oct 25, 2023 · 1 comment
Open

getData dependency inside useEffect #132

spalt08 opened this issue Oct 25, 2023 · 1 comment

Comments

@spalt08
Copy link

spalt08 commented Oct 25, 2023

As a developer I expect getData function to be immutable, so I can use it as a dependency inside useEffect. However it seems like getData function is re-created on each render.

Simple example

function Component({ changingPropName }) {
  const { getData } = useVisitorData()
  
  useEffect(() => {
    console.log('effect triggered', getData)
  }, [getData])

  return <div>{changingPropName}</div>
}

function Emulate() {
  const [counter, setCounter] = useState(1);

  useEffect(() => {
    setInterval(() => setCounter((state) => state + 1), 1000)
  }, [setCounter])

  return <Component changingPropName={counter} />
}

ReactDOM.render(<Emulate />, document.body)
@spalt08
Copy link
Author

spalt08 commented Oct 25, 2023

Workaround I am using:

// eslint-disable-next-line react-hooks/exhaustive-deps
const getDataMemo = useMemo(() => getData, [])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant