Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Build performant React Native and Expo apps with best practices for lists, animations, and navigation
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
rules/animation-derived-value.md
1---2title: Prefer useDerivedValue Over useAnimatedReaction3impact: MEDIUM4impactDescription: cleaner code, automatic dependency tracking5tags: animation, reanimated, derived-value6---78## Prefer useDerivedValue Over useAnimatedReaction910When deriving a shared value from another, use `useDerivedValue` instead of11`useAnimatedReaction`. Derived values are declarative, automatically track12dependencies, and return a value you can use directly. Animated reactions are13for side effects, not derivations.1415**Incorrect (useAnimatedReaction for derivation):**1617```tsx18import { useSharedValue, useAnimatedReaction } from 'react-native-reanimated'1920function MyComponent() {21const progress = useSharedValue(0)22const opacity = useSharedValue(1)2324useAnimatedReaction(25() => progress.value,26(current) => {27opacity.value = 1 - current28}29)3031// ...32}33```3435**Correct (useDerivedValue):**3637```tsx38import { useSharedValue, useDerivedValue } from 'react-native-reanimated'3940function MyComponent() {41const progress = useSharedValue(0)4243const opacity = useDerivedValue(() => 1 - progress.get())4445// ...46}47```4849Use `useAnimatedReaction` only for side effects that don't produce a value50(e.g., triggering haptics, logging, calling `runOnJS`).5152Reference:53[Reanimated useDerivedValue](https://docs.swmansion.com/react-native-reanimated/docs/core/useDerivedValue)54