React Native

How do you implement a modal in React Native?

Medium
13
Added
You can implement a modal in React Native using the Modal component provided by React Native.

Solution Code

React Native
-- Example Code --
import React, { useState } from "react";
import { View, Text, Button, Modal } from "react-native";

const App = () => {
  const [modalVisible, setModalVisible] = useState(false);

  return (
    <View>
      <Modal
        animationType="slide"
        transparent={true}
        visible={modalVisible}
        onRequestClose={() => {
          setModalVisible(!modalVisible);
        }}
      >
        <View style={{ flex: 1, justifyContent: "center", alignItems: "center" }}>
          <Text>Hello Modal!</Text>
          <Button title="Close" onPress={() => setModalVisible(false)} />
        </View>
      </Modal>
      <Button title="Open Modal" onPress={() => setModalVisible(true)} />
    </View>
  );
};

export default App;
Explanation
This code demonstrates how to use the Modal component to create a modal.

Guided Hints

{'hint': 'Use the Modal component for modals.'}
{'hint': 'Control the visibility with state.'}