LoginSignup
3
2

More than 3 years have passed since last update.

React Native: コンポーネントで遷移したい

Last updated at Posted at 2019-12-10

React Nativeで画面遷移するときは、navigationを使います
別の画面に遷移したいときは、画面のpropsに{ navigation }を渡して、navigation.navigate('AnotherScreen')のようにして遷移させます

が、これができるのはscreens配下にある画面だけで、コンポーネントではできません

コンポーネントの中から画面遷移を呼び出したいときは、react-nativewithNavigation関数を使います

手順

  1. import する
  2. export するときに withNavigation を使う

サンプル


import * as React from 'react';
import { View, TouchableOpacity } from 'react-native';
import { withNavigation } from 'react-navigation'; // ここ大事

const MyComponent = ({ navigation }) => {
  return (
    <View>
      <TouchableOpacity onPress={() => { navigation.navigate('MyScreen') }}>
         <Text>遷移するよ</Text>
      </TouchableOpacity>
    </View>
  )
}

export default withNavigation(MyComponent); // ここ大事

参考

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