fix: detached worklet handlers on Fabric when StrictMode
enabled
#579
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
📜 Description
On fabric with
StrictMode
enabled worklet handlers were not working. It was because of the fact that effects are fired twice, and current implementation ofuseSyncEffect
simply added handler and unmounted it when effect is fired twice intentionally.💡 Motivation and Context
Original PR that introduced these changes was #538
Theoretically this problem could be fixed by using a version like:
But in this case:
StrictMode
will run it asynchronously anyway because it'll be triggered fromuseEffect
);StrictMode
fix can not be covered by unit tests, which indicated that we go too deeply and probably doing something wrong if it can not be covered by unit tests.So keeping that in my head I decided to make a step back and rewrite this part - now instead of
useSyncEffect
I will useuseLayoutEffect
. In this case I'll still run effects slightly earlier than plainuseEffect
and it should be alright for now. I've tested Expensify app with these changes and now I don't see advantages of usinguseSyncEffect
overuseLayoutEffect
- both of them give roughly speaking the same result.I also thought about using
useInsertionEffect
(I'm not css-in-js library developer, but still) - but in this case I also didn't discover any advantages and react warns that it's better to stick to defaultuseEffect
/useLayoutEffect
hooks. Potentially we can useuseInsertionEffect
but it should be a last resort and for now I prefer to useuseSyncEffect
😅Important
Even though we migrated to default lifecycles it still worth to note, that fix #575 is still relevant, as
ref
may still not be updated by the time when effect is called 🤷♂️📢 Changelog
JS
useSynceEffect
hook;useLayoutEffect
instead ofuseEffect
hook.🤔 How Has This Been Tested?
Tested on iPhone 15 Pro, iOS 17.5 (FabricExample app in
StrictMode
and Expensify app).📝 Checklist