How to compare shared value inside areEqualProps? #6221
-
How to compare a SharedValue property inside memo callback?
Is this possible or not possible? Which one is the correct way? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Hey! In general, you shouldn't rely on the The correct solution is 2 Long explanationEven though comparing const progress1 = useSharedValue(0);
const progress2 = useSharedValue(0); During the initial render: <MyComponent animatedProgress={progress1} /> Some time later, when the component re-renders: <MyComponent animatedProgress={progress2} /> // you pass a different progress value In this case, when You shouldn't rely on the Short explanation of the difference between JS and UI threadReanimated uses the concept of 2 separate threads:
Let's say you want to update the Unfortunately, if you read the value from the JS thread (not in a worklet function executed within runOnUI, useDerivedValue, useAnimatedReaction or other hook from reanimated, which executes the callback directly on the UI thread), you may not get the expected This is the result of asynchronous way of communication between JS and UI thread which doesn't guarantee that value is the same in both of them at the same time. All React hooks are executed on the JS thread, so comparison of shared values via |
Beta Was this translation helpful? Give feedback.
-
Thx very much @MatiPl01! ❤️ |
Beta Was this translation helpful? Give feedback.
Hey!
In general, you shouldn't rely on the
SharedValue
read via the.value
prop, so 3 and 4 are incorrect. Similarly, the first one proposed solution is incorrect, because lodash makes deep comparison of objects, so it will compare.value
prop.The correct solution is 2
Long explanation
Even though comparing
.value
from the JS thread is technically incorrect, in your case, all of listed proposed solution should work the same unless you pass a completely differentSharedValue
to your component, e.g. like this:During the initial render:
Some time later, when the compone…