Help us understand the problem. What is going on with this article?

ReactNativeでモーダルを使う

普段Navigation関係はReact-Navigationを利用していて、React-NavigationにもModalを出す機能はあるが、StackNavigationを入れ子にする必要がある等、ちょっと癖があるので、react-native-modalを試してみます。

やりたいこと

下記のようなモーダルを出す。

スクリーンショット 2019-09-15 8.50.38.png

準備

npm install --save react-native-modal

実装

これで動く。
基本的に<Modal></Modal>でコンテンツを囲ってやればいい。

App.js
import React from 'react';
import { StyleSheet, Text, View, SafeAreaView, Button } from 'react-native';
import Modal from "react-native-modal";


export default class App extends React.Component {

    state = {
        isModalVisible: false
    }

    toggleModal = () => {
        this.setState({ isModalVisible: !this.state.isModalVisible });
    }

    render() {
        return (
            <SafeAreaView style={{ flex: 1 }}>
                <Text>App</Text>
                <Button
                    title="Show modal"
                    onPress={this.toggleModal}
                />
                <Modal isVisible={this.state.isModalVisible}>
                    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: "#fff" }}>
                        <Text>Modal Content</Text>
                        <Button
                            title="Close modal"
                            onPress={this.toggleModal}
                        />
                    </View>
                </Modal>
            </SafeAreaView>
        );
    }
}

応用(コンテンツの外だし)

Modalで表示する内容が複雑になってくると、メイン?コンテンツの中に定義するのはうざい。
Modalで表示する内容を外部ファイルにしてみる。

Child.js

基本的には<View></View>部分を切り出しただけだが、モーダルを閉じるためのメソッドをprops経由で受けとっている。

Child.js
import React from 'react';
import { StyleSheet, Text, View, SafeAreaView, Button } from 'react-native';

export default class Child extends React.Component {

    render() {
        return (
            <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: "#fff" }}>
                <Text>Modal Content</Text>
                <Button
                    title="Close modal"
                    // イベントをpropsでもらう
                    onPress={this.props.nav.toggleModal}
                />
            </View>
        );
    }
}

App.js

Child.jsを読み込み、this全体をnavという名前でpropsで渡している。

App.js
import React from 'react';
import { StyleSheet, Text, View, SafeAreaView, Button } from 'react-native';
import Modal from "react-native-modal";

import Child from './Child';

export default class App extends React.Component {

    state = {
        isModalVisible: false
    }

    toggleModal = () => {
        this.setState({ isModalVisible: !this.state.isModalVisible });
    }

    render() {
        return (
            <SafeAreaView style={{ flex: 1 }}>
                <Text>App</Text>
                <Button
                    title="Show modal"
                    onPress={this.toggleModal}
                />
                <Modal isVisible={this.state.isModalVisible}>
                    <Child nav={this} />
                </Modal>
            </SafeAreaView>
        );
    }
}

モーダルの大きさ

もちろん、モーダル自体の大きさもstyleで調整可能。

App.js
import React from 'react';
import { StyleSheet, Text, View, SafeAreaView, Button } from 'react-native';

export default class Child extends React.Component {

    render() {
        return (
            <View style={{ justifyContent: 'center', alignItems: 'center', backgroundColor: "#fff", height:300 }}>
                <Text>Modal Content</Text>
                <Button
                    title="Close modal"
                    // イベントをpropsでもらう
                    onPress={this.props.nav.toggleModal}
                />
            </View>
        );
    }
}

スクリーンショット 2019-09-15 9.00.04.png

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away