How to use setState in worklet
See original GitHub issueDescription
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:
- Created 3 years ago
- Reactions:7
- Comments:7 (5 by maintainers)
Top 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 >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 FreeTop 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
Top GitHub Comments
@CDBridger hey 👋 I think you missed the full error message there. There should be more:
https://docs.swmansion.com/react-native-reanimated/docs/api/runOnJS
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 😃