React-native-reanimated: How can I JS state when animation end

Created on 22 Nov 2020  路  4Comments  路  Source: software-mansion/react-native-reanimated

Description

I read all docs and know the third parameter of withTiming, withSpring is an end callback of animation.
But when I try to set a state of React hook from useState, then the error is invoked.

I understand what is the problem in the thread level. currently, it seems there is no way to update JS state from UI state even if directing as a worklet; or using runOnJS.

Is there any way? Thank you.

Environment

    "react": "16.13.1",
    "react-native": "0.63.3",
    "react-native-reanimated": "2.0.0-alpha.9.1",
    "react-native-redash": "15.11.1",
鉂換uestion 馃彔 Reanimated2

Most helpful comment

Define state change callback on the React side and then execute that callback using runOnJS inside the animation's callback like so:

withTiming(1, null, () => {
  runOnJS(setStateCallback)();
})

All 4 comments

Issue validator - update # 2

Hello!
Congratulations! Your issue passed the validator! Thank you!

Define state change callback on the React side and then execute that callback using runOnJS inside the animation's callback like so:

withTiming(1, null, () => {
  runOnJS(setStateCallback)();
})

Thank you for your answer. +1

@terrysahaidak how do u pass arguments to runOnJS function.
sorry: nevermind.

runOnJS(setStateCallback)(args)

Was this page helpful?
0 / 5 - 0 ratings