Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • 1
    Thanks! Saved my day. Ideal for calling functions once, but only after some state needs to be loaded. Commented Apr 13, 2021 at 7:17
  • I actually got here because i got curious if there is such a thing which has this ref logic built in so you don't have to create a ref inside of a component but hook is taking care of it for you? Commented Nov 2, 2022 at 11:08
  • ref is required for conditional logic. if you want to run a code after something, this hook will handle it for you. Please check the second example. Commented Nov 4, 2022 at 4:00
  • Thanks! This is what worked for me infact, for anyone developing React(v18) in strict mode, this is the only way to get around calling effects once! The previously popular notion that passing an empty array as useEffect deps will cause the effect to be called once is no longer true react.dev/reference/react/… Commented Aug 2, 2024 at 22:19
  • This should be the accepted answer. Using state can result in a race condition as state is set asynchronously. Commented Mar 16, 2025 at 21:03