kotlin-wrappers icon indicating copy to clipboard operation
kotlin-wrappers copied to clipboard

useEffectOnce / useLayoutEffectOnce / useRef broken when re-rendering at an above component

Open Lezzio opened this issue 3 years ago • 4 comments

Hi,

When using these hooks in a component, whenever we trigger a re-render from the above components it doesn't remember it has been fired once already and loses its "memory". Note that the re-render needs to induce DOM modifications (node add/delete as far as I know) for the bug to happen.

The expected behavior is the following on React JS : https://codesandbox.io/s/kind-sea-45wqs?file=/src/App.tsx But it instead acts just like this : https://codesandbox.io/s/kind-sea-45wqs?file=/src/App.tsx

It's what we've at least observed in our project and we reproduced the bug this way :

Main component with the state update and children: image

OnceComponent : image

ParentComponent : image

Console output after pressing the button multiple times : image

The minimal way to work around this issue is by using a unique key on the uppermost components next to the state being updated (direct children) :

Works image

Works image

NB : Slack thread on the issue https://kotlinlang.slack.com/archives/C5ZTZ6ER0/p1628553043020800

Lezzio avatar Aug 10 '21 18:08 Lezzio

@Lezzio @PeredurOmega Do you use function components?

turansky avatar Aug 17 '21 14:08 turansky

Almost exclusively

PeredurOmega avatar Aug 17 '21 14:08 PeredurOmega

This issue is reopened because we still have problems with keys in wrappers and we have plans to introduce some solution.

sgrishchenko avatar Sep 11 '21 14:09 sgrishchenko

Workarounds unlocked by #1264

turansky avatar Feb 24 '22 22:02 turansky

Next step - compiler plugin! :)

turansky avatar Aug 11 '23 20:08 turansky

Compiler plugin with conditional rendering support is ready!

turansky avatar Oct 12 '23 00:10 turansky