Ask questionsBug: useLayoutEffect callback called twice when a dom node is in a dependency array
<!-- Please provide a clear and concise description of what the bug is. Include screenshots if needed. Please test using the latest version of the relevant React packages to make sure your issue has not already been fixed. -->
React version: v16.3.1
<!-- Your bug will get fixed much faster if we can run your code and it doesn't have dependencies other than React. Issues without reproduction steps or code examples may be immediately closed as not actionable. -->
Link to code example: https://codepen.io/everdimension/pen/rNOpZGK
<!-- Please provide a CodeSandbox (https://codesandbox.io/s/new), a link to a repository on GitHub, or provide a minimal code example that reproduces the problem. You may provide a screenshot of the application if you think it is relevant to your bug report. Here are some tips for providing a minimal example: https://stackoverflow.com/help/mcve. -->
useLayoutEffect callback gets called a second time, although the values in the deps array have not changed
useLayoutEffect callback should not get called unless values in the dependency array change
Answer questions everdimension
ref.current is undefined initially
Actually, it is defined, AFAU that's the point of useLayoutEffect: by the time it's run, the dom had already been commited. I added an
if with an early return to my example to demonstrate that.
You are not supposed to use ref.current as a dependency
I thought that was what official react hooks linter recommended? If that's the wrong way, then what's the proper way to update effect if the dom node changes or gets destroyed?