3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Expo(ReactNative)でSplashの表示時間(消すタイミング)を制御する

Last updated at Posted at 2019-09-07

expoでsplashの表示を制御したい。

  • SplashScreen.preventAutoHide()
  • SplashScreen.Hide()

とかを組み合わせてるとできるみたい。詳しくはこちらを。

とりあえず、3秒待ってみる。

実際はコンテンツのロードとかを待つ感じかと。

componentDidMount()でSplash画面が自動的に消えるのを阻止し、後はお好みのタイミングでSplashScreen.Hide()とする。

App.js
import React from 'react';
import { StyleSheet, Text, View, Image } from 'react-native';
import { SplashScreen } from 'expo';

export default class App extends React.Component {

    //Splashのステータス管理(標準false)
    state = {
        isReady: false,
    }

    //Splashが自動的に消えるのを防止
    componentDidMount() {
        SplashScreen.preventAutoHide();
    }

    render() {
        //ステータスがfalseなら
        if (!this.state.isReady) {
            return (
                <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
                    <Image
                        source={require('./assets/splash.png')} //いちおうSplashと同じ画像を表示(関係ないみたい)
                        onLoad={this.updateAsync} //読み込みが終わったらCallbackをキック
                    />
                </View>
            );
        }

        //標準画面(Splash後に表示)
        return (
            <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
                <Text>hoge</Text>
            </View>
        );
    }

    //コールバック
    //3秒待って、splash画面を隠す。そしてステータスをtrueに。
    updateAsync = async () => {
        await sleep(3000);
        SplashScreen.hide();
        this.setState({ isReady: true })
    }
}

//sleepコマンド作成
export const sleep = (sec) => {
    return new Promise(resolve => {
        setTimeout(resolve, sec);
    })
}
3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?