React Native Technology & Digital Applications 4 — Questions and Answers
Question 1: How does `useEffect` with an empty dependency array behave in a React Native component?
- It runs once after the component mounts and the cleanup function runs on unmount (Correct answer)
- It runs on every render of the component
- It runs only when the component unmounts
- It runs before the component renders for the first time
Correct answer: It runs once after the component mounts and the cleanup function runs on unmount
An empty dependency array [] tells React to run the effect only once after the initial render, equivalent to componentDidMount in class components.
Question 2: Which Expo module lets a React Native app access and save images to the device camera roll?
- expo-media-library (Correct answer)
- expo-camera-roll
- expo-image-picker only
- expo-file-system
Correct answer: expo-media-library
expo-media-library provides APIs to read from and save assets to the device's photo library on both iOS and Android.
Question 3: What is the purpose of the `react-native-reanimated` library in React Native apps?
- To run animations entirely on the UI thread, avoiding bridge serialization jank (Correct answer)
- To replace the Animated API with CSS-based animations
- To provide pre-built animation presets like lottie files
- To schedule animations using requestAnimationFrame on the JS thread
Correct answer: To run animations entirely on the UI thread, avoiding bridge serialization jank
Reanimated 2+ uses worklets that execute on the UI thread, producing smooth 60/120fps animations even when the JS thread is busy.
Question 4: In React Native, which approach correctly prevents a TextInput from being obscured by the soft keyboard on iOS?
- Wrap the screen in KeyboardAvoidingView with behavior='padding' (Correct answer)
- Set keyboardType='none' on the TextInput
- Use ScrollView with keyboardDismissMode='on-drag'
- Add paddingBottom to the root View equal to the keyboard height
Correct answer: Wrap the screen in KeyboardAvoidingView with behavior='padding'
On iOS, KeyboardAvoidingView with behavior='padding' adds bottom padding equal to the keyboard height, keeping inputs visible.
Question 5: What does the `Pressable` component offer that `TouchableOpacity` does not?
- Granular pressed-state styling, hit slop configuration, and ripple effects via the style callback (Correct answer)
- Built-in accessibility roles without extra props
- Cross-platform gesture recognition with velocity tracking
- Automatic debouncing of rapid taps
Correct answer: Granular pressed-state styling, hit slop configuration, and ripple effects via the style callback
Pressable accepts a style function with a pressed argument, enabling fine-grained control over appearance on press, along with configurable hit areas.
Question 6: Which command regenerates native iOS files after changing dependencies in a React Native project?
- npx pod-install (or pod install inside the ios/ directory) (Correct answer)
- react-native run-ios --reset-cache
- npx react-native upgrade
- xcodebuild clean
Correct answer: npx pod-install (or pod install inside the ios/ directory)
pod install (via npx pod-install or directly inside the ios folder) fetches and links CocoaPods dependencies whenever the Podfile changes.
Question 7: What is the difference between `ScrollView` and `FlatList` in React Native?
- ScrollView renders all children at once; FlatList renders items lazily as they scroll into view (Correct answer)
- FlatList renders all items upfront; ScrollView renders lazily
- ScrollView only works horizontally; FlatList only works vertically
- FlatList requires a fixed item height; ScrollView supports dynamic heights
Correct answer: ScrollView renders all children at once; FlatList renders items lazily as they scroll into view
ScrollView mounts all child elements immediately, while FlatList virtualizes the list and only renders items near the visible viewport.
How does `useEffect` with an empty dependency array behave in a React Native component?