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.

How to use setState in worklet

See original GitHub issue

Description

how to change state or same in reanimated v2 events

Code

  const x = useSharedValue(0);
  const [state , setState] = React.useState(false);
  const gestureHandler = useAnimatedGestureHandler({
    onStart: (_, ctx) => {
      ctx.startX = x.value;
    },
    onActive: (event, ctx) => {
      x.value = ctx.startX + event.translationX;
    },
    onEnd: _ => {
      x.value = withSpring(0);
      setState(true); // <- how to use this here 
    },
  });

Package versions

  • React:
  • React Native:
  • React Native Reanimated: 2.0.0-alpha.5

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:7
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

7reactions
karol-bisztygacommented, Dec 18, 2020

@CDBridger hey 👋 I think you missed the full error message there. There should be more:

Solution is:
a) If you want to synchronously execute this method, mark it as a Worklet
b) If you want to execute this method on the JS thread, wrap it using runOnJS

https://docs.swmansion.com/react-native-reanimated/docs/api/runOnJS

5reactions
terrysahaidakcommented, Aug 28, 2020

Yeah @karol-bisztyga, as well as setState(v => !v) I couldn’t get to work. But I mean the provided code still works as expected 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

React native run useState/force rerender inside worklet funtion
I just found out that reanimated offers the function runOnJS which makes it possible to run a javscript function like setState inside a ......
Read more >
useAnimatedReaction | React Native Reanimated
This matters when, for instance, worklet uses values dependent on the component's state. Example: const App = () => { const [state, setState]...
Read more >
How to use Reanimated 2 (a beginners guide)
To spawn this worklet we're going to use the useAnimatedStyle hook. This hook will return a style object that updates with randomNumber ....
Read more >
How to refactor React components to use Hooks
setState calls everywhere in the component, we've chosen to take a simpler, incremental approach that doesn't involve a lot of code changes.
Read more >
Getting started with Reanimated 2 for React native Part-1
Problem arises while using the core animated api is that animated Api ... Each worklet function can be run either on the main...
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