This UI component uses React Native Reanimated to create smooth, native-driven animations. It animates a box that moves horizontally when you press a button, showing how animations can improve app interactivity and feel.
Widget Tree
View
├── Animated.View
└── Button
The root View contains two children: an Animated.View which is the animated box, and a Button that triggers the animation. The Animated.View visually moves horizontally on the screen when the button is pressed.
Render Trace - 4 Steps
Step 1: View
Step 2: Animated.View
Step 3: Button
Step 4: Animated.SharedValue and withTiming
State Change - Re-render
Trigger:User presses the 'Move Box' button
Before
animatedValue is 0, box is centered
→
After
animatedValue is 150, box shifted right by 150 units
Re-renders:Animated.View re-renders with updated transform; Button and parent View remain unchanged
UI Quiz - 3 Questions
Test your understanding
What happens when the 'Move Box' button is pressed?
AThe red box slides smoothly to the right.
BThe button disappears.
CThe screen background color changes.
DNothing happens.
Key Insight
React Native Reanimated lets you create smooth, performant animations by running them on the native side, avoiding slow JavaScript timers. This improves user experience by making UI interactions feel natural and responsive.
Practice
(1/5)
1.
What is the main purpose of useSharedValue in React Native Reanimated?
easy
A. To hold and update animation values that can change over time
B. To create static styles for components
C. To handle user input events
D. To navigate between screens
Solution
Step 1: Understand useSharedValue role
useSharedValue is designed to store values that can change and animate smoothly.
Step 2: Compare with other options
Static styles, user inputs, and navigation are unrelated to useSharedValue.
Final Answer:
To hold and update animation values that can change over time -> Option A
Quick Check:
useSharedValue = hold animation values [OK]
Hint: Remember: useSharedValue stores changing animation data [OK]
Common Mistakes:
Confusing useSharedValue with static styling
Thinking it handles user input or navigation
Assuming it triggers animations directly
2.
Which of the following is the correct syntax to create an animated style using useAnimatedStyle?
D. const animatedStyle = useAnimatedStyle(() => [ opacity: sharedValue.value ]);
Solution
Step 1: Check arrow function syntax
Returning an object directly requires parentheses around the object: () => ({ ... }).
Step 2: Validate each option
const animatedStyle = useAnimatedStyle(() => ({ opacity: sharedValue.value })); uses correct syntax with parentheses. Options A, C, and D have syntax errors.
Hint: Always use .value to read shared values inside animated styles [OK]
Common Mistakes:
Forgetting .value on shared values
Calling useSharedValue inside effects incorrectly
Misunderstanding transform property format
5.
You want to create a button that scales up smoothly when pressed and returns to normal size when released. Which approach correctly uses React Native Reanimated to achieve this?
const scale = useSharedValue(1);
const animatedStyle = useAnimatedStyle(() => ({
transform: [{ scale: scale.value }]
}));
// On press in
scale.value = withTiming(1.2, { duration: 200 });
// On press out
scale.value = withTiming(1, { duration: 200 });
hard
A. This approach correctly animates scale on press in and out using withTiming
B. You must use useState instead of useSharedValue for animation
C. withTiming cannot be used inside event handlers
D. Animated styles cannot animate transform properties
Solution
Step 1: Check useSharedValue and useAnimatedStyle usage
Using useSharedValue to hold scale and useAnimatedStyle to apply transform is correct.
Step 2: Verify animation with withTiming
withTiming smoothly animates the scale value on press in and out events.
Step 3: Confirm event handler usage
Assigning scale.value = withTiming(...) inside press handlers is valid and triggers animation.
Final Answer:
This approach correctly animates scale on press in and out using withTiming -> Option A