What's the right way to use RunOnJS?
See original GitHub issueDescription
I’ve looked at both the PR that added a note on RunOnJS (p.s. this doesn’t appear in the docs) and I’ve looked at the example found here. However I still get the redbox that says Tried to synchronously call function {bound dispatchAction} from a different thread....
What I’m trying to achieve, is to update local state once the animation completes.
Code
export function someAnimation(callback?: (isCancelled: boolean) => void) {
'worklet';
return withSequence(
withTiming(1, {duration: 300, easing: Easing.linear}),
withDelay(100, withTiming(2, {duration: 500, easing: Easing.linear}, callback)),
);
}
// Inside a functional component
const [value, setValue] = React.useState(false);
// Trigger it here on first render
React.useEffect(() => {
shared.value = someAnimation(runOnJS(() => setValue(true)));
}, []);
I’ve tried multiple variants (based on the two links above) to no avail. i.e.
// And then I just tried doing random stuff in hopes it works
function setTheValue() { //including const declaration
'worklet'; // and without 'worklet'
setValue(true);
}
// Also
shared.value = someAnimation(runOnJS(() => { 'worklet'; setValue(true)}));
Package versions
- React: 16.13.1
- React Native:0.63.3
- React Native Reanimated: 2.0.0-alpha.8 & 2.0.0-alpha.9 (tried both)
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:11 (3 by maintainers)
Top Results From Across the Web
runOnJS | React Native Reanimated
runOnJS. When you call a function on the UI thread you can't be sure if you're calling a worklet or a callback from...
Read more >When should we use runOnUI and runOnJS?
You can use runOnJS and runOnUI to schedule execution of a function on a different thread than you currently are, i.e. you should...
Read more >Reanimated 2 - the new approach to creating animations in ...
Find out the new hooks, build custom useSlider hook, and get an understanding of animation techniques in this step by step guide.
Read more >Trying to understand the internals of Reanimated 2
Before talking about the internals of reanimated 2 I would like to talk about how our UI is handled in an app, how...
Read more >react-native-reanimated runOnJS TypeScript Examples
The following examples show how to use react-native-reanimated.runOnJS. ... __worklet) { return runOnUI(cb); } return runOnJS(cb); }. Example #2 ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

The following works
This doesn’t work either!