Can we run anything inside worklet? #4968
Unanswered
IslamRustamov98
asked this question in
Q&A
Replies: 3 comments 3 replies
-
You definitely can. Check the following example: import React from 'react';
import { View, Button, StyleSheet, Text } from 'react-native';
import Animated, {
runOnJS,
runOnUI,
useAnimatedStyle,
useSharedValue,
} from 'react-native-reanimated';
export default function App() {
const sharedValue = useSharedValue(0);
let plainValue = 0;
function assignPlainValue(value: number) {
plainValue = value;
}
function heavyComputation() {
'worklet';
for (let i = 0; i < 100000000; i++) {
// NOOP
}
sharedValue.value = sharedValue.value === 0 ? 1 : 0;
runOnJS(assignPlainValue)(sharedValue.value);
}
const style = useAnimatedStyle(() => {
return {
opacity: sharedValue.value,
};
});
return (
<View style={styles.container}>
<Button title="Compute" onPress={runOnUI(heavyComputation)} />
<Button
title="Log plain value"
onPress={() => {
console.log('plainValue:', plainValue);
}}
/>
<Animated.View style={[styles.box, style]} />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
box: {
width: 100,
height: 100,
backgroundColor: 'red',
},
}); What exactly happens here:
|
Beta Was this translation helpful? Give feedback.
2 replies
-
I couldn't allocate three.js matrices in a worklet (crash) |
Beta Was this translation helpful? Give feedback.
1 reply
-
I tried once to turn a function to get the color palette from an image into a worklet and run in the UI thread, it didn't turn out well, it blocked the UI thread completely. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Let's say that, for example, I need to do some heavy computations with JS, not related to animation. Can I create a workletized function that does it? Or worklets are allowed to work only with animations.
Beta Was this translation helpful? Give feedback.
All reactions