What will happen when the button inside this React Native modal is pressed?
import React, { useState } from 'react'; import { View, Text, Button, Modal } from 'react-native'; export default function App() { const [visible, setVisible] = useState(false); return ( <View> <Button title="Show Modal" onPress={() => setVisible(true)} /> <Modal visible={visible} transparent={true} animationType="slide"> <View> <Text>Hello Modal</Text> <Button title="Close" onPress={() => setVisible(false)} /> </View> </Modal> </View> ); }
Look at how the visible state changes on button presses.
The visible state controls the modal's visibility. Pressing 'Show Modal' sets it to true, showing the modal. Pressing 'Close' sets it to false, hiding the modal.
In React Native, which method is commonly used to dismiss a bottom sheet component when the user taps outside it?
Think about how to detect taps outside the bottom sheet area.
To dismiss a bottom sheet on outside tap, a transparent overlay with a touchable area is used. When tapped, it triggers a function to close the sheet.
What happens to the child components inside a React Native Modal when the modal is hidden (visible=false)?
Consider how React Native handles components inside a modal when visibility changes.
When visible is false, the modal hides its content but keeps child components mounted in memory.
Which code snippet correctly toggles a bottom sheet's visibility state in React Native?
const [isVisible, setIsVisible] = useState(false);
Remember how to update state using the setter function from useState.
The setter function returned by useState is called with the new value. Assigning or calling methods on it is incorrect.
Given this code, why does the modal not close when pressing the 'Close' button?
import React, { useState } from 'react'; import { View, Text, Button, Modal } from 'react-native'; export default function App() { const [visible, setVisible] = useState(false); return ( <View> <Button title="Show Modal" onPress={() => setVisible(true)} /> <Modal visible={visible} transparent={true} animationType="slide"> <View> <Text>Hello Modal</Text> <Button title="Close" onPress={() => setVisible(false)} /> </View> </Modal> </View> ); }
Check how the onPress function is written for the 'Close' button.
The onPress handler should call setVisible(false). Writing onPress={() => setVisible} only passes the function without calling it, so the state does not change.