Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dependency array; removes shared value access warning from react-native-reanimated #710

Merged
merged 4 commits into from
Nov 23, 2024

Conversation

nmassey
Copy link
Contributor

@nmassey nmassey commented Oct 23, 2024

Fixes #706

What: the bug

Where in our code

In ScrollViewGesture.tsx, we had included touching.value and scrollEndTranslation.value in the dependency arrays for a couple of React callbacks. Not only is this not necessary, but it also causes a warning message to appear (and introduces the potential performance implication mentioned below).

These dependency array .values were introduced in eb21293 (as a part of #116).

Warning message in react-native-reanimated

As per #706 (thank you, @joaofelippe911), a console warning appears when using react-native-reanimated version 3.16.0 or higher. This new warning was added via software-mansion/react-native-reanimated#6310

The warning looks like:

 WARN  [Reanimated] Reading from `value` during component render. Please ensure that you do not access the `value` property or use `get` method of a shared value while React is rendering a component.

If you don't want to see this message, you can disable the `strict` mode. Refer to:
https://docs.swmansion.com/react-native-reanimated/docs/debugging/logger-configuration for more details.

Why: potential performance implication

As per https://docs.swmansion.com/react-native-reanimated/docs/core/useSharedValue/#remarks ,

When you read the sv.value on the JavaScript thread, the thread will get blocked until the value is fetched from the UI thread. In most cases it will be negligible, but if the UI thread is busy or you are reading a value multiple times, the wait time needed to synchronize both threads may significantly increase.

What: the fix

Don't depend on touching.value and scrollEndTranslation.value in our dependency arrays in useCallback().

These aren't needed: all accesses to touching.value and scrollEndTranslation.value are within worklets, and as per https://docs.swmansion.com/react-native-reanimated/docs/core/useSharedValue/#remarks :

When you change the sv.value the update will happen synchronously on the UI thread.

Verification: in local testing, the warning disappears!

In my local testing, this makes the warning disappear completely! And the carousel still behaves as normal.

Aside: where is this code used anyway?

In ScrollViewGesture, there is a useAnimatedReaction() which calls resetBoundary() for changes to translation.value whenever pagingEnabled is false.

resetBoundary() calls activeDecay() in some cases... which in turn calls onFinish() in some cases.

It is worth noting that onFinish, activeDecay, and resetBoundary are only ever used when pagingEnabled is false.

(After a few minutes of playing around with my code for my use case, I was not able to cause activeDecay() to ever run.)

Copy link

changeset-bot bot commented Oct 23, 2024

🦋 Changeset detected

Latest commit: 86b8308

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
react-native-reanimated-carousel Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

vercel bot commented Oct 23, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
react-native-reanimated-carousel ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 5, 2024 8:41pm

@dosubot dosubot bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Oct 23, 2024
@dosubot dosubot bot added size:S This PR changes 10-29 lines, ignoring generated files. and removed size:XS This PR changes 0-9 lines, ignoring generated files. labels Oct 23, 2024
@nmassey nmassey changed the title fix: remove shared value access warning from react-native-reanimated fix: dependency array; removes shared value access warning from react-native-reanimated Oct 23, 2024
@MatkoMilic
Copy link

I still get these warnings on newest version?

@Faruktulumcu
Copy link

Are there any updates on this pull request? Do we know if it’s going to be merged?

@el-lsan
Copy link

el-lsan commented Nov 1, 2024

Would be great if we can have this merged
Thanks

@BrodaNoel
Copy link
Contributor

Thanks for this fix! It's killing the console on Expo SDK 52 beta

@bigriot
Copy link

bigriot commented Nov 12, 2024

Do we have an ETA on when this will be merged?

@angelo-hub
Copy link

bump on this too

@LukasMod
Copy link

LukasMod commented Nov 12, 2024

For me this fix did not help with all warnings. I had to remove other .value from arrays. Here is my patch fix for 3.5.1

react-native-reanimated-carousel+3.5.1.patch

@nmassey
Copy link
Contributor Author

nmassey commented Nov 15, 2024

For me this fix did not help with all warnings. I had to remove other .value from arrays. Here is my patch fix

react-native-reanimated-carousel+3.5.1.patch

hi @LukasMod - it looks like your patch is for an older version (3.5.1) than the current mainline branch. I think those other .values are no longer relevant to what's on main.

@BrodaNoel
Copy link
Contributor

This is another reason why we need to release 4.0.0 ASAP.

@ansh
Copy link

ansh commented Nov 22, 2024

Please release this ASAP! @dohooo

@dohooo dohooo merged commit d98bb99 into dohooo:main Nov 23, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size:S This PR changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Warning when using Reanimated version 3.16
10 participants