Modals and bottom sheets help show extra information or options without leaving the current screen. They keep the user focused and make the app easier to use.
Modal and bottom sheet in React Native
Introduction
Syntax
React Native
import { Modal, View, Text, Button } from 'react-native'; <Modal visible={modalVisible} animationType="slide" transparent={true} onRequestClose={() => setModalVisible(false)} > <View style={styles.centeredView}> <View style={styles.modalView}> <Text>Modal Content</Text> <Button title="Close" onPress={() => setModalVisible(false)} /> </View> </View> </Modal>
Modal is a component that appears over the current screen.
Use
visible prop to show or hide the modal.Examples
React Native
import { Modal, View, Text } from 'react-native'; <Modal visible={true} animationType="fade"> <View> <Text>Hello Modal</Text> </View> </Modal>
React Native
import { Modal, View, Text, Button } from 'react-native'; <Modal visible={modalVisible} animationType="slide" transparent={true}> <View style={{flex:1, justifyContent:'flex-end'}}> <View style={{backgroundColor:'white', padding:20, borderTopLeftRadius:20, borderTopRightRadius:20}}> <Text>Bottom Sheet Content</Text> <Button title="Close" onPress={() => setModalVisible(false)} /> </View> </View> </Modal>
Sample App
This app shows a button. When pressed, a modal slides up with a message and a close button. The background dims to focus on the modal.
React Native
import React, { useState } from 'react'; import { Modal, View, Text, Button, StyleSheet } from 'react-native'; export default function App() { const [modalVisible, setModalVisible] = useState(false); return ( <View style={styles.container}> <Button title="Show Modal" onPress={() => setModalVisible(true)} /> <Modal visible={modalVisible} animationType="slide" transparent={true} onRequestClose={() => setModalVisible(false)} > <View style={styles.centeredView}> <View style={styles.modalView}> <Text style={styles.modalText}>Hello from Modal!</Text> <Button title="Close" onPress={() => setModalVisible(false)} /> </View> </View> </Modal> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center' }, centeredView: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: 'rgba(0,0,0,0.5)' }, modalView: { backgroundColor: 'white', padding: 20, borderRadius: 10, alignItems: 'center' }, modalText: { marginBottom: 15, fontSize: 18 } });
Important Notes
Always provide a way to close the modal, like a button or tapping outside if allowed.
Use transparent prop to make the background see-through for bottom sheets.
Modals block interaction with the rest of the app until closed.
Summary
Modals show content over the current screen to keep user focus.
Bottom sheets are modals that slide up from the bottom for actions or info.
Use visible prop to control showing or hiding modals.
Practice
1. What is the main purpose of a
Modal component in React Native?easy
Solution
Step 1: Understand what a Modal does
A Modal appears on top of the current screen and blocks interaction with the background.Step 2: Compare options
Only To display content over the current screen and keep user focus describes this behavior correctly; others describe unrelated features.Final Answer:
To display content over the current screen and keep user focus -> Option AQuick Check:
Modal = Overlay content [OK]
Hint: Modals block background, focus user attention [OK]
Common Mistakes:
- Confusing Modal with navigation
- Thinking Modal is a footer or background task
- Assuming Modal is always full screen
2. Which of the following is the correct way to control the visibility of a
Modal in React Native?easy
Solution
Step 1: Recall the correct prop for Modal visibility
The React Native Modal uses thevisibleprop to show or hide the modal.Step 2: Check each option
Only<Modal visible={true}>usesvisiblecorrectly; others use incorrect prop names.Final Answer:
<Modal visible={true}> -> Option BQuick Check:
Modal visibility prop = visible [OK]
Hint: Use visible prop to toggle Modal display [OK]
Common Mistakes:
- Using show or isVisible instead of visible
- Confusing display with visible
- Forgetting to pass boolean value
3. What will happen when the following React Native code runs?
const [modalVisible, setModalVisible] = React.useState(false);
return (
<>
<Button title="Show Modal" onPress={() => setModalVisible(true)} />
<Modal visible={modalVisible} animationType="slide">
<View>
<Text>Hello Modal</Text>
<Button title="Close" onPress={() => setModalVisible(false)} />
</View>
</Modal>
</>
);medium
Solution
Step 1: Understand state controlling Modal visibility
The state variablemodalVisiblestarts false, so modal is hidden initially.Step 2: Analyze button actions
Pressing 'Show Modal' setsmodalVisibleto true, showing modal with slide animation. 'Close' button sets it back to false, hiding modal.Final Answer:
The modal appears sliding up when 'Show Modal' is pressed and closes when 'Close' is pressed -> Option AQuick Check:
State controls visible with animation [OK]
Hint: State true shows modal; false hides it [OK]
Common Mistakes:
- Assuming modal shows without pressing button
- Ignoring animationType effect
- Thinking modal cannot be closed
4. Identify the error in this React Native Modal usage:
const [show, setShow] = React.useState(false);
return (
<Modal visible={show} animationType="fade" onRequestClose={() => setShow(false)}>
<View>
<Text>Modal Content</Text>
</View>
</Modal>
<Button title="Open" onPress={() => setShow(true)} />
);medium
Solution
Step 1: Check JSX structure
The return statement has two sibling elements: Modal and Button, which is invalid JSX without a wrapper.Step 2: Identify correct fix
Wrapping Modal and Button inside a fragment or View fixes the error.Final Answer:
Modal must be wrapped inside a single parent element -> Option CQuick Check:
JSX requires one parent element [OK]
Hint: Wrap siblings in a fragment or View [OK]
Common Mistakes:
- Ignoring JSX syntax rules
- Thinking onRequestClose is optional on Android
- Confusing visible prop type
5. You want to create a bottom sheet modal that slides up from the bottom and closes when tapping outside it. Which combination of props and components achieves this behavior in React Native?
hard
Solution
Step 1: Understand bottom sheet modal requirements
It must slide up (animationType="slide"), be transparent so background is visible, and detect taps outside to close.Step 2: Identify correct props and components
Usingtransparent={true}allows background taps. Wrapping background in TouchableWithoutFeedback lets you detect taps to close modal.Step 3: Eliminate wrong options
Options B, C, and D miss transparency, correct animation, or tap detection.Final Answer:
Use <Modal visible={true} animationType="slide" transparent={true}> with a TouchableWithoutFeedback wrapping the background to detect taps -> Option DQuick Check:
Slide + transparent + tap detection = bottom sheet [OK]
Hint: Slide + transparent + tap outside closes modal [OK]
Common Mistakes:
- Forgetting transparent prop for background tap
- Using fade animation instead of slide
- Not handling tap outside to close
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
