react icon indicating copy to clipboard operation
react copied to clipboard

Bug: In StrictMode callback refs are called once

Open domarmstrong opened this issue 3 years ago • 0 comments

Hi. I'm running in to an issue in StrictMode with the fact that React is calling effects multiple times but then is only calling ref callbacks once. I believe this could be related to (if not the same issue as) https://github.com/facebook/react/issues/24670.

React version: 18.2.0

Steps To Reproduce

The code is contrived but it shows how it can introduce confusion.

https://codesandbox.io/s/react-18-playground-forked-b3ql27?file=/src/App.tsx

The current behavior

Callback refs are only called once even though effect cleanup is called on mount.

The expected behavior

Callback refs are called again on simulated re-mount.

Unfortunately dealing with StrictMode so far has not been a very nice DX. It seems to have added a whole additional cognitive overhead. It also seems to raise all sorts of very opaque bugs that take a bit of working through often without obvious solutions. Although I understand the reasoning it feels like a loaded footgun.

domarmstrong avatar Oct 10 '22 17:10 domarmstrong