1. 程式人生 > >react-navigation之StackNavigator使用方法及附上超級詳細樣例程式碼

react-navigation之StackNavigator使用方法及附上超級詳細樣例程式碼

react-navigation 之 StackNavigator

網上雖說有很多篇關於react-navigation的使用說明的文章和部落格,不過我找了大半天也不見詳細具體的使用方法,大部分都是介紹種種屬性的,為此鄙人自告奮勇,整理了一份比較詳細的有關react-navigation的使用樣例程式碼,本篇文章先介紹StackNavigator的使用方法。

StackNavigator

實現不同頁面之間的跳轉

限於篇幅,有關StackNavigator的各種屬性以及頁面間傳值這裡不再贅述,程式碼裡已經寫了,請自行百度或谷歌。

不多說,先上程式碼,先乾為敬,程式碼下方會有詳細說明:

說明:樣例的版本是0.53

下面程式碼貼上即可使用

1. App.js

import React, { Component } from 'react';
import {
  StyleSheet,
} from 'react-native';
import Pages from './src/Pages';
type Props = {};
export default class App extends Component<Props> {
  render() {
    return (
      <Pages/>
    );
  }
}

App.js大家都很熟悉,react native的啟動頁,這裡將所有頁面的整合路由頁Pages.js頁面當做元件引用進來,相信大家都知道怎麼做,不再贅述,如有不清楚的 請到這裡來: 

react native 自定義元件以及引用

2. Pages.js

import React, { Component } from 'react';
import FirstPage from './FirstPage';
import SecondPage from './SecondPage';
import ThirdPage from './ThirdPage';
import FourthPage from './FourthPage';
import {
    StackNavigator,
} from 'react-navigation';
export default class Pages extends Component{
    constructor(props){
        super(props);
    }
    render(){
        return(
            <SimpleAppNavigator/>
        )
    }
}
const SimpleAppNavigator = StackNavigator({
    page1: {screen: FirstPage},
    page2: {screen: SecondPage},
    page3: {screen: ThirdPage},
    page4: {screen: FourthPage}
},{
     initialRouteName: 'page1',
 });

(下面的說明不再是樣例中的程式碼)

Pages.js是所有的頁面的集合頁面:

(1) 將所有的子頁面匯入進來

import FirstPage from './FirstPage';
import SecondPage from './SecondPage';
import ThirdPage from './ThirdPage';
import FourthPage from './FourthPage';

(2) 然後通過StackNavigator註冊路由

const SimpleAppNavigator = StackNavigator({
    page1: {screen: FirstPage},
    page2: {screen: SecondPage},
    page3: {screen: ThirdPage},
    page4: {screen: FourthPage}
},{
     initialRouteName: 'page1',
 });

(3) 然後在render裡返回 SimpleAPPNavigator即可:

render(){
        return(
            <SimpleAppNavigator/>
        )
    }

3. FirstPage.js

import React, { Component } from 'react';
import {
    StyleSheet,
    Text,
    View,
    TouchableOpacity
} from 'react-native';
export default class FirstPage extends Component {
    static navigationOptions = {
        title: 'page 1',
    };
    render() {
        const { navigate } = this.props.navigation;
        return (
            <View style={styles.container}>
                <Text style={styles.welcome}>
                    This is First Page!
                </Text>
                <TouchableOpacity
                    onPress={()=>{
                        navigate('page2',{message:'Hello,我是page1!'});
                    }}
                >
                    <Text style={styles.textView}>
                        Go to second page
                    </Text>
                </TouchableOpacity>
                <TouchableOpacity
                    onPress={()=>{
                        navigate('page3',{message:'Hello,我是page1!'});
                    }}
                >
                    <Text style={styles.textView}>
                        Go to third page
                    </Text>
                </TouchableOpacity>
                <TouchableOpacity
                    onPress={()=>{
                        navigate('page4',{message:'Hello,我是page1!'});
                    }}
                >
                    <Text style={styles.textView}>
                        Go to fourth page
                    </Text>
                </TouchableOpacity>
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#F5FCFF',
    },
    welcome: {
        fontSize: 20,
        textAlign: 'center',
        margin: 10,
    },
    textView: {
        fontSize: 16,
        textAlign: 'center',
        margin: 10,
        color:'red'
    },
});

4. SecondPage.js

import React, { Component } from 'react';
import {
    StyleSheet,
    Text,
    View,
    TouchableOpacity
} from 'react-native';
export default class SecondPage extends Component<Props> {
    static navigationOptions = {
        title: 'page 2',
    };
    render() {
        const { navigate } = this.props.navigation;
        const { params } = this.props.navigation.state;
        return (
            <View style={styles.container}>
                <Text style={styles.welcome}>
                    This is Second Page!
                </Text>
                <TouchableOpacity
                    onPress={()=>{
                        this.props.navigation.goBack()
                    }}
                >
                    <Text style={styles.textView}>
                        Touch me to go back!
                    </Text>
                </TouchableOpacity>
                <TouchableOpacity
                    onPress={()=>{
                        navigate('page1');
                    }}
                >
                    <Text style={styles.textView}>
                        Go to First page
                    </Text>
                </TouchableOpacity>
                <TouchableOpacity
                    onPress={()=>{
                        navigate('page3');
                    }}
                >
                    <Text style={styles.textView}>
                        Go to third page
                    </Text>
                </TouchableOpacity>
                <TouchableOpacity
                    onPress={()=>{
                        navigate('page4');
                    }}
                >
                    <Text style={styles.textView}>
                        Go to fourth page
                    </Text>
                </TouchableOpacity>
                <Text style={styles.textView}>
                    {'傳來的引數是:' + params.message}
                </Text>
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#F5FCFF',
    },
    welcome: {
        fontSize: 20,
        textAlign: 'center',
        margin: 10,
    },
    textView: {
        fontSize: 16,
        textAlign: 'center',
        margin: 10,
        color:'red'
    },
});

5. ThirdPage.js

import React, { Component } from 'react';
import {
    StyleSheet,
    Text,
    View,
    TouchableOpacity
} from 'react-native';
export default class ThirdPage extends Component<Props> {
    static navigationOptions = {
        title: 'page 3',
    };
    render() {
        const { navigate } = this.props.navigation;
        return (
            <View style={styles.container}>
                <Text style={styles.welcome}>
                    This is Third Page!
                </Text>
                <TouchableOpacity
                    onPress={()=>{
                        this.props.navigation.goBack()
                    }}
                >
                    <Text style={styles.textView}>
                        Touch me to go back!
                    </Text>
                </TouchableOpacity>
                <TouchableOpacity
                    onPress={()=>{
                        navigate('page1');
                    }}
                >
                    <Text style={styles.textView}>
                        Go to second page
                    </Text>
                </TouchableOpacity>
                <TouchableOpacity
                    onPress={()=>{
                        navigate('page2');
                    }}
                >
                    <Text style={styles.textView}>
                        Go to Second page
                    </Text>
                </TouchableOpacity>
                <TouchableOpacity
                    onPress={()=>{
                        navigate('page4');
                    }}
                >
                    <Text style={styles.textView}>
                        Go to fourth page
                    </Text>
                </TouchableOpacity>
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#F5FCFF',
    },
    welcome: {
        fontSize: 20,
        textAlign: 'center',
        margin: 10,
    },
    textView: {
        fontSize: 16,
        textAlign: 'center',
        margin: 10,
        color:'red'
    },
});

6. FourthPage.js

import React, { Component } from 'react';
import {
    StyleSheet,
    Text,
    View,
    TouchableOpacity
} from 'react-native';
export default class FourthPage extends Component<Props> {
    static navigationOptions = {
        title: 'page 4',
    };
    render() {
        const { navigate } = this.props.navigation;
        return (
            <View style={styles.container}>
                <Text style={styles.welcome}>
                    This is Fourth Page!
                </Text>
                <TouchableOpacity
                    onPress={()=>{
                        this.props.navigation.goBack()
                    }}
                >
                    <Text style={styles.textView}>
                        Touch me to go back!
                    </Text>
                </TouchableOpacity>
                <TouchableOpacity
                    onPress={()=>{
                        navigate('page1');
                    }}
                >
                    <Text style={styles.textView}>
                        Go to second page
                    </Text>
                </TouchableOpacity>
                <TouchableOpacity
                    onPress={()=>{
                        navigate('page3');
                    }}
                >
                    <Text style={styles.textView}>
                        Go to third page
                    </Text>
                </TouchableOpacity>
                <TouchableOpacity
                    onPress={()=>{
                        navigate('page1');
                    }}
                >
                    <Text style={styles.textView}>
                        Go to First page
                    </Text>
                </TouchableOpacity>
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#F5FCFF',
    },
    welcome: {
        fontSize: 20,
        textAlign: 'center',
        margin: 10,
    },
    textView: {
        fontSize: 16,
        textAlign: 'center',
        margin: 10,
        color:'red'
    },
});

附上專案結構:

執行效果如下:

相關推薦

react-navigationStackNavigator使用方法附上超級詳細程式碼

react-navigation 之 StackNavigator 網上雖說有很多篇關於react-navigation的使用說明的文章和部落格,不過我找了大半天也不見詳細具體的使用方法,大部分都是介紹種種屬性的,為此鄙人自告奮勇,整理了一份比較詳細的有關react-nav

React-Native開發六 react-navigationStackNavigator簡介

1 前言 react-navigation是RN開發中一款開源的導航元件,它的功能包括StackNavigator,TabNavigator,DrawerNavigator。react-navigation的出現替代了Navigator、 Ex-Navigation等老一代的導航元件

React-navigationStackNavigator

1 先給出資料:    參考:資料一、資料二 有問題找 issues, 基本上坑都在這裡。 2 導航欄分類 按使用形式主要分三部分: (1) StackNavigator: 類似於普通的N

React-Native開發九 react-navigationAndroid的打包與釋出

1前言 RN的開發中正式釋出前需要打包與簽名,然後才能上架各家應用市場。打包需要將js與圖片資原始檔打包進apk檔案中,生成index.android.bundle與index.android.bundle.meta檔案。下面介紹RN開發中打包APK的主要步驟,IOS沒研究過,不再本

React-Native開發八 react-navigation自定義元件Counter

1 前言 我們知道RN中任何介面元素都可以看成元件,小到一個按鈕,大到一個頁面。RN開發就是不停的開發元件和使用元件,並讓他們協同工作,這樣高效率協同的執行起來,這樣就能完成一個APP的功能了 在實際的開發中,我們經常需要自定義一些滿足我們專案開發的自定義元件,類似於Android

React-Native開發七 react-navigationAsyncStorage資料儲存

1 前言 我們都知道,在Android和IOS中分別有不同的持久化資料方式,例如Android中的檔案,資料庫,SharePrefences等。AsyncStorage是一個簡單的key-value儲存系統,是RN官方推薦的。它儲存的都是String型別的資料,是一個RN中輕量級的資

react-redux connect 方法詳解

Redux 是「React 全家桶」中極為重要的一員,它試圖為 React 應用提供「可預測化的狀態管理」機制。Redux 本身足夠簡單,除了 React,它還能夠支援其他介面框架。所以如果要將 Redux 和 React 結合起來使用,就還需要一些額外的工具,其中最重要的莫過於 react-redux 了。

oc學習setter方法getter方法,簡單的命名規則

#import <Foundation/Foundation.h>@interface  Student :NSObject//類名的首字母大寫,以後每個單詞的首字母大寫{//例項變數的定義,命名通常以_開頭,第一個單詞的首字母小寫,以後每個單詞的首字母大寫   

react-navigation動態修改title的內容

效果圖: 動態修改title內容: static navigationOptions = { title: ({ state }) => `Chat with ${s

React-navigationTabNavigation

完整程式碼: import React from 'react'; import { AppRegistry, Text, View, Button, ScrollView,

react-navigationTabNavigator使用

1、效果圖: index.android.js: /** * Sample React Native App * https://github.com/facebook/react-native

React Native內部方法常用幾種寫法和呼叫規則

1 簡單部分程式碼 export default class App extends Component<Props> { render() { return (

大數據測試hadoop單機環境搭建(超級詳細版)

com jvm 末尾 內容 取數 搭建 cluster replicat specific 友情提示:本文超級長,請備好瓜子 Hadoop的運行模式 單機模式是Hadoop的默認模式,在該模式下無需任何守護進程,所有程序都在單個JVM上運行,該模式主要用於開發和調試map

004-spring-data-elasticsearch 3.0.0.0使用【二】-spring-data定義方法、創建repository實、從聚合根發布事件

-- ble sch current 4.3 ... reference tex manager 續上文 1.4、定義方法   存儲庫代理有兩種方法可以從方法名稱派生特定於存儲的查詢。它可以直接從方法名稱派生查詢,或者使用手動定義的查詢。可用選項取決於實際store。但

iOS巔峰CocoaPods安裝使用的詳細步驟

一、概要 iOS開發時,專案中會引用許多第三方庫,CocoaPods(https://github.com/CocoaPods/CocoaPods)可以用來方便的統一管理這些第三方庫。 二、安裝 由於網上的教程基本都大同小異,但細節之處還不是很完善,所以藉機會在這裡補充下: 注:要使用CocoaPods,那

大資料測試hadoop單機環境搭建(超級詳細版)

友情提示:本文超級長,請備好瓜子   Hadoop的執行模式 單機模式是Hadoop的預設模式,在該模式下無需

TensorFlow入門MNIST程式碼分析

這幾天想系統的學習一下TensorFlow,為之後的工作打下一些基礎。看了下《TensorFlow:實戰Google深度學習框架》這本書,目前個人覺得這本書還是對初學者挺友好的,作者站在初學者的角度講解TensorFlow,所以比較容易理解。這篇博文主要是為了分析其中的一個經

React學習旅----事件方法改變this指向

import React, { Component } from 'react'; import '../assets/css/index.css' // react繫結屬性注意點 // class要換成className // for 要換成htmlFor // style class Home

react-navigation 中無法找到 CardStackStyleInterpolator 的問題解決方法

由於專案需要,在使用react-native進行app開發時,產品要求ios和android介面的頁面跳轉動畫要統一。由於ios原生方法就是從右向左進行跳轉,回退從左向右跳轉。android原生的跳轉方式與ios的不同,是從底部到頂部漸變渲染跳轉的,回退是從頂部到地圖渲染跳轉。查看了好多相關文章,找

"永恒藍"勒索軟件病毒防範方法措施

下載地址 信息安全 武器庫 校園網 目前微軟已發布補丁MS17-010修復了“永恒之藍”攻擊的系統漏洞,請廣大師生盡快根據各自操作系統安裝補丁,地址:https://technet.microsoft.com/zh-cn/library/security/MS17-010;對於XP、2003等