React Native Reanimated is a library that helps create smooth and powerful animations in React Native apps by running animation code on the native thread, making animations faster and more fluid.
Click to reveal answer
beginner
What is a shared value in React Native Reanimated?
A shared value is a special kind of variable that holds animation state and can be safely updated on the native side. It allows animations to run smoothly without blocking the JavaScript thread.
Click to reveal answer
intermediate
Explain the purpose of the useAnimatedStyle hook.
The useAnimatedStyle hook lets you create styles that automatically update when shared values change. It connects animation values to the style properties of components.
Click to reveal answer
intermediate
Why does React Native Reanimated run animations on the native thread?
Running animations on the native thread avoids delays caused by JavaScript processing. This means animations stay smooth even if the JavaScript thread is busy doing other work.
Click to reveal answer
beginner
What is the role of the withTiming function?
withTiming is used to create a timing animation that changes a shared value smoothly over a set duration, like moving a box from left to right in a given time.
Click to reveal answer
Which hook is used to create animated styles in React Native Reanimated?
AuseState
BuseAnimatedStyle
CuseEffect
DuseRef
✗ Incorrect
useAnimatedStyle creates styles that update automatically when shared values change.
What does a shared value in Reanimated represent?
AA variable that holds animation state on the native thread
BA React component
CA CSS style
DA JavaScript event handler
✗ Incorrect
Shared values hold animation data and run on the native thread for smooth animations.
Why is running animations on the native thread beneficial?
AIt disables animations
BIt makes the app use more memory
CIt makes animations smoother by avoiding JavaScript delays
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