question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

What's the right way to use RunOnJS?

See original GitHub issue

Description

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:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

26reactions
Andariuscommented, Nov 17, 2020

The following works

function updateShare(){
  setValue(true)
}
function callback(isCancelled: boolean){
  'worklet'
  runOnJS(updateShare)()
}

React.useEffect(() => {
    shared.value = someAnimation(callback)
}, [])
2reactions
dhirajanand014commented, Nov 23, 2020

@Andarius this does work, thanks. A bit convoluted.

I simplified it to the following, which still seemed to work

function callback(isCancelled: boolean){
  'worklet'
  runOnJS(setValue)(true);
}

React.useEffect(() => {
    shared.value = someAnimation(callback)
}, [])

even further simplified to

React.useEffect(() => {
    shared.value = someAnimation(() => { 
      'worklet';
      runOnJS(setValue)(true)
    });
}, [])

I’ll need to do some further digging to understand when and where to use worklet.

Once again, thanks for the help!

This doesn’t work either!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found