Navigator使用,參數(shù)傳遞

1、index.android.js
/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */
import React, {Component} from 'react';
import {
    AppRegistry,
    View,
    Navigator,
    Text,
    BackAndroid,
    StyleSheet,
    ToolbarAndroid,
    TouchableHighlight
} from 'react-native';
import Login from './components/Login';

var _navigator;// 全局變量
BackAndroid.addEventListener('hardwareBackPress', () => {
    if (_navigator && _navigator.getCurrentRoutes().length > 1) {
        _navigator.pop();
        return true;
    }
    return false;
});

// 控制頁面如何跳轉(zhuǎn)的總樞紐
let RouteMapper = function (route, navigationOperations) {
    _navigator = navigationOperations;
    let Component_ = route.component;
    if(Component_){
        return <Component_ {...route.params} navigator={_navigator}/>
    }
};

export default class Home extends Component {
    render() {
        let firstPageRouter = 'login';
        let firstComponent = Login;
        return (
            <Navigator
                style={styles.container}
                initialRoute={{name: firstPageRouter,component:firstComponent}}
                renderScene={RouteMapper}
            />
        );
    }
}

var styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#593012',
    },
});

AppRegistry.registerComponent('Home', () => Home);

2、Login.js
/**
 * Created by Administrator on 2017/2/23.
 */
import React, {Component, PropTypes} from 'react';

import {
    AppRegistry,
    View,
    Navigator,
    Text,
    BackAndroid,
    StyleSheet,
    ToolbarAndroid,
    TouchableHighlight
} from 'react-native';

import MainScene from './MainScene';

export default class Login extends Component {

    static defaultProps = {
        navigator:null,
    }

    constructor(props) {
        super(props);
        this.state = {
            user : null,
        }
    }

    login(){
        let this_ = this;
        this.props.navigator.push({
            name: 'main',
            component:MainScene,
            params:{//傳給其他頁面的參數(shù),這些個(gè)參數(shù)是作為props傳給下個(gè)頁面的
                title: '這是從登錄跳轉(zhuǎn)而來',
                id : 1,
                // 為了獲取下個(gè)頁面返回給這個(gè)頁面的數(shù)據(jù),這里使用了一個(gè)回調(diào)函數(shù)
                getUser:function (user) {
                    this_.setState({
                        user:user,
                    });
                }
            },
        });
    }

    render() {
        if(this.state.user){
            return(
                <View>
                    <Text style={{fontSize:50}}>
                        用戶信息: { JSON.stringify(this.state.user) }
                    </Text>
                </View>
            );
        }else {
            return (
                <View>
                    <TouchableHighlight style={{margin: 50}} onPress={this.login.bind(this)}>
                        <Text style={{width: 100, height: 60,fontSize:50}}>
                            登錄
                        </Text>
                    </TouchableHighlight>

                    <TouchableHighlight style={{margin: 50}}>
                        <Text style={{width: 100, height: 60,fontSize:50}}>
                            注冊(cè)
                        </Text>
                    </TouchableHighlight>
                </View>
            );
        }
    };
}
3、MainScene.js
/**
 * Created by Administrator on 2017/2/23.
 */
import React, {Component, PropTypes} from 'react';

import {
    AppRegistry,
    View,
    Navigator,
    Text,
    BackAndroid,
    StyleSheet,
    ToolbarAndroid,
    TouchableHighlight
} from 'react-native';


const USER = {
    1: {name : "lxr",age : 39},
    2: {name : "zbs",age : 67},
};

let onActionSelected = function (position) {
    if (position == 0) {
        console.log("===============> 患者列表")
    } else if (position == 1) {
        console.log("===============> 電子病歷")
    }
}

let onIconClick = function () {
    console.log("===============> onIconClick")
}

export default class MainScene extends Component {

    static defaultProps = {
        title: null,
        navigator:null,
    }

    constructor(props) {
        super(props);
    }

    toBack(){
        const id_ = this.props.id;
        const getUser = this.props.getUser;
        if(id_ && getUser){
            let user = USER[id_];
            getUser(user);
        }
        if(this.props.navigator){
            this.props.navigator.pop();
        }
    }

    render() {
        return (
            <View style={{flex: 1}}>
                <ToolbarAndroid
                    logo={require('./../imgs/test.png')}
                    navIcon={require('./../imgs/01_deault.png')}
                    title={this.props.title}
                    titleColor="white"
                    actions={[
                        {
                            title: '患者列表',
                            icon: require('./../imgs/02_deault.png'),
                            show: 'always',
                            showWithText: false
                        },
                        {
                            title: '電子病歷',
                            icon: require('./../imgs/03_deault.png'),
                            show: 'always',
                            showWithText: false
                        }
                    ]}
                    style={styles.toolbar}
                    onActionSelected={onActionSelected}
                    onIconClicked={onIconClick}// 這個(gè)是作用在navIcon選項(xiàng)上的
                />
                <TouchableHighlight style={{flex:1}} onPress={this.toBack.bind(this)}>
                    <Text style={{width: 600, height: 300,fontSize:50}}>
                        點(diǎn)擊我返回上一個(gè)頁面,攜帶有參數(shù)。
                    </Text>
                </TouchableHighlight>
            </View>
        );
    };
}

var styles = StyleSheet.create({
    toolbar: {
        backgroundColor: '#666666',
        height: 56,
    },
});
4、參考資料

1、ReactNative官方文檔
2、ReactNative 中文文檔
3、新手理解Navigator的教程

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容