Bug: useLayoutEffect callback called twice when a dom node is in a dependency array
See original GitHub issueReact version: v16.3.1
Steps To Reproduce
- add
useLayoutEffect
- add dom node to the deps array
- invoke setState inside useLayoutEffect callback
Link to code example: https://codepen.io/everdimension/pen/rNOpZGK
The current behavior
useLayoutEffect
callback gets called a second time, although the values in the deps array have not changed
The expected behavior
useLayoutEffect
callback should not get called unless values in the dependency array change
Issue Analytics
- State:
- Created 3 years ago
- Comments:10 (3 by maintainers)
Top Results From Across the Web
React Hooks: useEffect() is called twice even if an empty array ...
It is the feature of ReactJS while we use React.StrictMode. StrictMode activates additional checks and warnings for its descendants nodes.
Read more >React 18 - Avoiding Use Effect Getting Called Twice
For React Hooks in React 18, this means a useEffect() with zero dependencies will be executed twice. Here is a custom hook that...
Read more >Hooks API Reference - React
Returns a memoized callback. Pass an inline callback and an array of dependencies. useCallback will return a memoized version of the callback that...
Read more >Avoiding useEffect with callback refs - TkDodo's blog
Interacting with DOM nodes doesn't necessarily need useEffect. ... The empty dependency array is okay because the only thing used inside is ...
Read more >React Hooks Common Mistakes - Bugfender
import React, { useEffect, useState, useCallback } from "react"; import ". ... Either include it or remove the dependency array.
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
@bvaughn
If you have to clean up when your DOM node changes, you have two options. First is to use effects, which means that you HAVE to put the node in state:
The second is to do it manually:
While you can certainly use the second option, and even wrap it in some sort of hook, I would rather just go with the first one.
There is no feasible way to detect when such a ref changes. But IMO this is fine. When a hook accepts a DOM ref it is a clear signal to the user, that this ref should not change until the component is unmounted and they should structure their code accordingly.