React native reanimated , application crashs when call setState in callback with withTiming or withSpring
Asked Answered
O

0

9

My React Native expo application crashing without any error when call function() in withTiming() callback

Example :

         
    const whenFinishFunction = () => {
        // do some thing
      };

    const [animationState, setAnimationState] = useState(false);
    
    progress.value = withTiming(1,{duration: 200},
          () => {
           whenFinishFunction();
           setAnimationState(false);
          }
        );

Solved by use runOnJS

like :

    progress.value = withTiming(1,{duration: 200},
          () => {
            runOnJS(whenFinishFunction)();
            runOnJS(setAnimationState)(false);
          }
        );
Optimistic answered 2/10, 2022 at 18:14 Comment(1)
It's worth nothing, for anybody coming across this, that withTiming and runOnJS are very picky about how you use them. It has to be an arrow function literal for the withTiming callback, and the only thing inside the literal has to be runOnJS called with a single locally defined function reference, and any passed arguments (as shown in the example).Cyprian

© 2022 - 2024 — McMap. All rights reserved.