home / skills / sovranbitcoin / sovran / animation-with-react-compiler

animation-with-react-compiler skill

/.agents/skills/animation-with-react-compiler

This skill guides using React Native Reanimated shared values with React Compiler by applying get() and set() for compatibility and safety.

npx playbooks add skill sovranbitcoin/sovran --skill animation-with-react-compiler

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
2.6 KB
---
name: animation-with-react-compiler
description: Provides React Native Reanimated guidelines for using shared values with React Compiler. Applies to tasks involving useSharedValue, shared values, React Compiler compatibility, or accessing/modifying shared value values.
---

# Animation with React Compiler

## Overview

Guidelines for using React Native Reanimated shared values with React Compiler. When using React Compiler, you must use the `get()` and `set()` methods instead of directly accessing the `value` property to ensure compatibility with React Compiler standards.

## When to Apply

Reference these guidelines when:
- Working with React Compiler enabled projects
- Using `useSharedValue` in components
- Accessing or modifying shared value values
- Ensuring React Compiler compatibility with Reanimated

## Key Guideline

### Use get() and set() Methods Instead of .value

When working with the React Compiler, you should refrain from accessing and modifying the `value` property directly. Instead, use the `get()` and `set()` methods. They're the alternative API for `useSharedValue`, compliant with the React Compiler standards.

**Don't do this** – accessing `.value` directly:

```tsx
function App() {
  const sv = useSharedValue(100);

  const animatedStyle = useAnimatedStyle(() => {
    "worklet";
    return { width: sv.value * 100 }; // ❌ Direct property access
  });

  const handlePress = () => {
    sv.value = sv.value + 1; // ❌ Direct property modification
  };
}
```

**Instead**, use `get()` and `set()` methods:

```tsx
function App() {
  const sv = useSharedValue(100);

  const animatedStyle = useAnimatedStyle(() => {
    "worklet";
    return { width: sv.get() * 100 }; // ✅ Using get() method
  });

  const handlePress = () => {
    sv.set((value) => value + 1); // ✅ Using set() method
  };
}
```

## Usage Patterns

### Reading Shared Values

```tsx
// ✅ In worklets (useAnimatedStyle, useDerivedValue, etc.)
const animatedStyle = useAnimatedStyle(() => {
  return { opacity: sv.get() };
});

// ✅ In useEffect or callbacks
useEffect(() => {
  console.log(sv.get());
}, []);
```

### Modifying Shared Values

```tsx
// ✅ Direct value assignment
sv.set(100);

// ✅ Using updater function
sv.set((currentValue) => currentValue + 1);

// ✅ With animation functions
sv.set(withSpring(1.2));
sv.set(withTiming(0.8, { duration: 300 }));
```

## Benefits

- **React Compiler Compatible**: Works seamlessly with React Compiler
- **Consistent API**: Provides a consistent method-based API
- **Type Safety**: Better TypeScript support and type inference
- **Future Proof**: Aligns with React Compiler standards and best practices

Overview

This skill provides clear, actionable guidelines for using React Native Reanimated shared values when a project uses the React Compiler. It explains why you should avoid direct .value access and shows the safe get()/set() alternatives for reading and updating shared values. The guidance focuses on compatibility, type safety, and predictable behavior with the React Compiler.

How this skill works

The skill inspects code patterns that read or write useSharedValue instances and recommends replacing direct .value access with sv.get() and sv.set(...). It covers reading inside worklets and effects, updating via direct values, updater functions, and animation wrappers (withSpring/withTiming). It enforces API usage that aligns with React Compiler constraints and TypeScript safety.

When to use it

  • Working in a project with React Compiler enabled
  • Implementing animations or derived values with useSharedValue
  • Accessing shared value data inside worklets (useAnimatedStyle, useDerivedValue, etc.)
  • Reading or logging shared values in effects or callbacks
  • Updating shared values in response to user interaction or animation events

Best practices

  • Always use sv.get() when reading a shared value instead of sv.value
  • Use sv.set(value) or sv.set(current => next) to modify shared values
  • Wrap animated updates with animation helpers: sv.set(withSpring(...)) or sv.set(withTiming(...))
  • Prefer updater functions (sv.set(current => ...)) for atomic, predictable increments
  • Keep reads inside worklets and useEffect as appropriate — get() is valid in both contexts

Example use cases

  • Animating width/opacity inside useAnimatedStyle using sv.get()
  • Incrementing a counter on button press with sv.set(current => current + 1)
  • Triggering spring/timing animations via sv.set(withSpring(...)) for smooth transitions
  • Logging a shared value in useEffect with sv.get() for debugging or persistence
  • Deriving values with useDerivedValue that read other shared values with get()

FAQ

Why not use sv.value directly?

Direct .value access is incompatible with React Compiler expectations and can break compiler optimizations and type inference. get()/set() ensure compatibility and predictable behavior.

Can I use get() outside worklets?

Yes. sv.get() works in worklets and in normal JS contexts like useEffect or callbacks. It provides a consistent read API across contexts.