2
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.

react-navigationで同一ページに遷移する

Last updated at Posted at 2019-10-31

#概要

普段はReact NativeでiOSアプリを開発してます

今回は、react-navigationで同一ページ(そのページ内での詳細は別)への遷移をした際に正常に動かせるまでのメモを共有します

#現象
ナビゲーターは以下の通り

navigator.js
import Page from "./page.js"
import { createStackNavigator} from "react-navigation";
const Rooter = createStackNavigator({
  Page: {
    screen: Page,
  },

});
export default Router;

アプリ登録は省略します、該当コンポーネントは以下の通り

Page.js
import React, { Component } from 'react';
import {
  TouchableOpacity,
  View,
  } from 'react-native'
class Page extends Component {

  render() {
  
      return(
        <View>
          <TouchableOpacity onPress={()=>{
             this.props.navigate.navigate("Page")
          }} >
            {"Button"}
          </TouchableOpacity>
        </View>

      );


  }
}
export default Page;

以上2点が主なコードです
ただこの構成ではうまく同一ページに遷移することができません

#解決
変更点はPage.jsのみです

Page.js
import React, { Component } from 'react';
import {
  TouchableOpacity,
  View,
  } from 'react-native'
class Page extends Component {

  render() {
  
      return(
        <View>
          <TouchableOpacity onPress={()=>{
             //this.props.navigate.navigate("Page")
             this.props.navigate.push("Page")
          }} >
            {"Button"}
          </TouchableOpacity>
        </View>

      );


  }
}
export default Page;

navigateをpushにすることで同一ページへの遷移を行わせることが可能になります

#参考資料
React Navigation API Reference: Navigator-dependent functions

stackoverflow: Stack same screen multiple times with React Navigation

2
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
2
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?