This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function SomeComponent() { | |
const randomWidth = useSharedValue(64); | |
const style = useAnimatedStyle(() => { | |
return { | |
// get the value carried by `randomWidth` SV by accessing value field | |
width: randomWidth.value, | |
}; | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
return ( | |
<View style={styles.container}> | |
{/* you can mix static RN styles and animated styles */} | |
<Animated.View style={[styles.box, style]} /> | |
</View> | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const style = useAnimatedStyle(() => { | |
// when defining method in one of the hooks from reanimated, you don't | |
// need to add 'worklet' directive. It is a worklet by default. | |
return { | |
width: 64, | |
}; | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function callback(text) { | |
console.log('Running on the RN thread', text); | |
} | |
function someWorklet() { | |
'worklet'; | |
console.log("I'm on UI but can call methods from the RN thread"); | |
callback('can pass arguments too'); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function returningWorklet() { | |
'worklet'; | |
return "I'm back"; | |
} | |
function someWorklet() { | |
'worklet'; | |
let what = returningWorklet(); | |
console.log('On the UI thread, other worklet says', what); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const width = 135.5; | |
function otherWorklet() { | |
'worklet'; | |
console.log('Captured width is', width); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function someWorklet(greeting) { | |
'worklet'; | |
console.log(greeting, 'From the UI thread'); | |
} | |
function onPress() { | |
runOnUI(someWorklet)('Howdy'); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function someWorklet(greeting) { | |
'worklet'; | |
console.log("Hey I'm running on the UI thread"); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import { StyleSheet, View } from 'react-native'; | |
import { PanGestureHandler, State } from 'react-native-gesture-handler'; | |
import Animated from 'react-native-reanimated'; | |
function Snappable() { | |
const dragStartX = useSharedValue(0); | |
const translateX = useSharedValue(0); | |
const boxStyle = useAnimatedStyle( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import { StyleSheet, View } from 'react-native'; | |
import { PanGestureHandler, State } from 'react-native-gesture-handler'; | |
import Animated from 'react-native-reanimated'; | |
function Snappable() { | |
const boxStyle = useAnimatedStyle( | |
context => { | |
return { | |
transform: [{ translateX: context.translateX }], |