┌─────────────────────────────┐ │ │ │ [ BOX ] │ │ │ │ │ │ [ Fade In/Out ] │ └─────────────────────────────┘
React Native Reanimated - Mini App: Build & Ship
import React from 'react'; import { View, Button, StyleSheet } from 'react-native'; import Animated, { useSharedValue, useAnimatedStyle, withTiming } from 'react-native-reanimated'; export default function FadeBox() { const opacity = useSharedValue(1); const animatedStyle = useAnimatedStyle(() => { return { opacity: opacity.value }; }); const toggleFade = () => { // TODO: Add code to toggle opacity between 0 and 1 with animation }; return ( <View style={styles.container}> <Animated.View style={[styles.box, animatedStyle]} /> <Button title="Fade In/Out" onPress={toggleFade} /> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#fff' }, box: { width: 150, height: 150, backgroundColor: '#4a90e2', marginBottom: 20 } });
import React from 'react'; import { View, Button, StyleSheet } from 'react-native'; import Animated, { useSharedValue, useAnimatedStyle, withTiming } from 'react-native-reanimated'; export default function FadeBox() { const opacity = useSharedValue(1); const animatedStyle = useAnimatedStyle(() => { return { opacity: opacity.value }; }); const toggleFade = () => { opacity.value = withTiming(opacity.value === 1 ? 0 : 1, { duration: 500 }); }; return ( <View style={styles.container}> <Animated.View style={[styles.box, animatedStyle]} /> <Button title="Fade In/Out" onPress={toggleFade} /> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#fff' }, box: { width: 150, height: 150, backgroundColor: '#4a90e2', marginBottom: 20 } });
We use useSharedValue to hold the opacity state. The useAnimatedStyle hook returns a style object that updates the opacity of the box based on the shared value. The toggleFade function changes the opacity value between 1 and 0 using withTiming to animate the change smoothly over 500 milliseconds. This creates a fade in/out effect when the button is pressed.
┌─────────────────────────────┐ │ │ │ [ BOX ] │ │ │ │ │ │ [ Fade In/Out ] │ └─────────────────────────────┘
Practice
What is the main purpose of useSharedValue in React Native Reanimated?
Solution
Step 1: Understand
useSharedValueroleuseSharedValueis designed to store values that can change and animate smoothly.Step 2: Compare with other options
Static styles, user inputs, and navigation are unrelated touseSharedValue.Final Answer:
To hold and update animation values that can change over time -> Option AQuick Check:
useSharedValue = hold animation values [OK]
- Confusing useSharedValue with static styling
- Thinking it handles user input or navigation
- Assuming it triggers animations directly
Which of the following is the correct syntax to create an animated style using useAnimatedStyle?
const animatedStyle = useAnimatedStyle(() => {
return {
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.Final Answer:
const animatedStyle = useAnimatedStyle(() => ({ opacity: sharedValue.value })); -> Option BQuick Check:
Arrow function returning object needs parentheses [OK]
- Missing parentheses around returned object
- Using return without braces properly
- Using array brackets instead of object braces
Given the code below, what will be the opacity of the Animated.View after the shared value updates to 0.5?
const opacity = useSharedValue(1);
const animatedStyle = useAnimatedStyle(() => ({
opacity: opacity.value
}));
// Later in code
opacity.value = 0.5;Solution
Step 1: Understand shared value update
Updatingopacity.valuedirectly sets the value immediately without animation.Step 2: Direct assignment causes instant jump
Reanimated updates the style instantly to 0.5 when using direct assignment, without smooth interpolation.Final Answer:
The opacity will instantly jump to 0.5 without animation -> Option DQuick Check:
Direct sharedValue.value = instant update [OK]
- Thinking the value stays the same after update
- Assuming it animates smoothly without withTiming
- Confusing with non-animated style updates
Identify the error in the following code snippet using React Native Reanimated:
const progress = useSharedValue(0);
const animatedStyle = useAnimatedStyle(() => {
transform: [{ translateX: progress }]
});Solution
Step 1: Check how shared values are accessed
InsideuseAnimatedStyle, shared values must be accessed with.value.Step 2: Identify the error in transform
The code usesprogressinstead ofprogress.value, causing a runtime error.Final Answer:
Missing .value when accessing shared value inside useAnimatedStyle -> Option CQuick Check:
Access sharedValue.value inside animated styles [OK]
- Forgetting .value on shared values
- Calling useSharedValue inside effects incorrectly
- Misunderstanding transform property format
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 });Solution
Step 1: Check useSharedValue and useAnimatedStyle usage
UsinguseSharedValueto hold scale anduseAnimatedStyleto apply transform is correct.Step 2: Verify animation with withTiming
withTimingsmoothly animates the scale value on press in and out events.Step 3: Confirm event handler usage
Assigningscale.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 AQuick Check:
UseSharedValue + withTiming = smooth press scale animation [OK]
- Using useState instead of useSharedValue for animation
- Thinking withTiming can't be used in event handlers
- Believing transform can't animate
Ready to prove it?
Pass the test and this pattern moves unknown
