|
| 1 | +--- |
| 2 | +id: reanimated |
| 3 | +title: Reanimated Integration |
| 4 | +sidebar_label: Reanimated Integration |
| 5 | +slug: /guides/reanimated |
| 6 | +--- |
| 7 | + |
| 8 | +import useBaseUrl from "@docusaurus/useBaseUrl"; |
| 9 | + |
| 10 | +:::warning |
| 11 | +Please make sure to follow the installation steps to configure your metro config to work with reanimated, worklets-core and react-native-filament: |
| 12 | + |
| 13 | +- [Installation Guide](./GETTING_STARTED.mdx) |
| 14 | + |
| 15 | +::: |
| 16 | + |
| 17 | +<a |
| 18 | + href="https://github.com/margelo/react-native-filament/blob/main/package/example/Shared/src/ReanimatedRotation.tsx" |
| 19 | + target="_blank" |
| 20 | + rel="noopener noreferrer" |
| 21 | +> |
| 22 | + <div class="image-container"> |
| 23 | + <svg xmlns="http://www.w3.org/2000/svg" width="283" height="535"> |
| 24 | + <image |
| 25 | + href={useBaseUrl("img/demo-reanimated.gif")} |
| 26 | + x="18" |
| 27 | + y="33" |
| 28 | + width="247" |
| 29 | + height="469" |
| 30 | + /> |
| 31 | + <image href={useBaseUrl("img/frame.png")} width="283" height="535" /> |
| 32 | + </svg> |
| 33 | + </div> |
| 34 | +</a> |
| 35 | + |
| 36 | +## react-native-worklets-core VS reanimated |
| 37 | + |
| 38 | +react-native-filament is build with [`react-native-worklets-core`](https://npmjs.com/package/react-native-worklets-core) which is a library that provides shared values and worklets for React Native. |
| 39 | +It's API is very similar to reanimated's, but it is not reanimated! |
| 40 | + |
| 41 | +In general, you pass shared values **from `react-native-worklets-core`** to the `react-native-filament` components to animate things like the position, rotation, scale, etc. |
| 42 | + |
| 43 | +However, many times you want to create animations using reanimated's API like `withTiming`, `withSpring`, etc. and then pass the resulting shared values to the `react-native-filament` components. |
| 44 | + |
| 45 | +## Using reanimated with react-native-filament |
| 46 | + |
| 47 | +To use reanimated animations / shared values with react-native-filament, **you need to sync the shared values from reanimated to a shared value from `react-native-worklets-core`**. |
| 48 | +You can do that using the [`useSyncSharedValue`](../api/functions/useSyncSharedValue) hook: |
| 49 | + |
| 50 | +```jsx |
| 51 | +import { useSyncSharedValue } from "react-native-filament"; |
| 52 | +import { useSharedValue, withTiming } from "react-native-reanimated"; |
| 53 | + |
| 54 | +// Create a reanimated shared value to drive the animation |
| 55 | +const opacity = useSharedValue(0); |
| 56 | + |
| 57 | +// Sync the reanimated shared value to a worklets-core shared value |
| 58 | +const workletsCoreOpacity = useSyncSharedValue(opacity); |
| 59 | + |
| 60 | +// Drive the animation using reanimated |
| 61 | +opacity.value = withTiming(1, { duration: 1000 }); |
| 62 | + |
| 63 | +// The worklets-core shared value will have the same value as the |
| 64 | +// reanimated shared value and can be used in react-native-filament components |
| 65 | +``` |
| 66 | + |
| 67 | +### Using derived values |
| 68 | + |
| 69 | +Sometimes you want to create a derived value from a reanimated shared value and use it in react-native-filament components. react-native-filament provides the [`useDerivedValue`](../api/functions/useDerivedValue) hook for that. |
| 70 | +It works like the `useDerivedValue` hook from reanimated, but just for worklets-core shared values: |
| 71 | + |
| 72 | +```jsx |
| 73 | +import { |
| 74 | + useSharedValue, |
| 75 | + withSequence, |
| 76 | + withSpring, |
| 77 | + withTiming, |
| 78 | +} from "react-native-reanimated"; |
| 79 | +import { useSyncSharedValue, useDerivedValue } from "react-native-filament"; |
| 80 | + |
| 81 | +// Create a reanimated shared value to drive the animation |
| 82 | +const animatedRotationY = useSharedValue(0); |
| 83 | + |
| 84 | +// RNF works with rn-worklets-core (RNWC), so create a |
| 85 | +// RNWC shared value thats synced with the reanimated shared value |
| 86 | +const rotationY = useSyncSharedValue(animatedRotationY); |
| 87 | + |
| 88 | +// This uses useDerivedValue from rn-filament to create a RNWC derived value (RNWC has no API for that yet) |
| 89 | +const rotation = |
| 90 | + useDerivedValue < |
| 91 | + Float3 > |
| 92 | + (() => { |
| 93 | + "worklet"; |
| 94 | + return [0, rotationY.value, 0]; |
| 95 | + }); |
| 96 | + |
| 97 | +// Run a animation: |
| 98 | +const spin = useCallback(() => { |
| 99 | + animatedRotationY.value = withSequence( |
| 100 | + withSpring(Math.PI * 2), |
| 101 | + withTiming(0, { duration: 0 }) |
| 102 | + ); |
| 103 | +}, [animatedRotationY]); |
| 104 | +``` |
0 commit comments