ReactNative——頁面跳轉


效果圖:

 

進入工作目錄,運行

react-native init NavigatorProject

創建項目NavigatorProject

 

 

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  TouchableHighlight,
  Image,
  Navigator
} from 'react-native';

 

class navigatorProject extends Component{
    render(){
        let defaultName = 'firstPageName';
        let defaultComponent = FirstPageComponent;
        return(             <Navigator                 initialRoute = {{name: defaultName, component: defaultComponent}}  //初始化導航器Navigator,指定默認的頁面                 configureScene = {                     (route) => {                         return Navigator.SceneConfigs.FloatFromRight;  //配置場景動畫,頁面之間跳轉時候的動畫                     }                 }                 renderScene = {                     (route, navigator) =>{                         let Component = route.component;                         return <Component{...route.params} navigator = {navigator} />  //渲染場景                     }                 }             />         );     } }

 

//第一個頁面

class FirstPageComponent extends Component{
    clickJump(){
        const{navigator} = this.props;
        if(navigator){
            navigator.push({  //navigator.push 傳入name和你想要跳的組件頁面
                name: "SecondPageComponent",
                component: SecondPageComponent
            });
        }  
    }

    render(){
        return(
            <View style = {styles.container}>
                <Text>我是第一個頁面</Text>
                <TouchableHighlight
                    underlayColor = "rgb(180,135,250)"
                    activeOpacity = {0.5}
                    style = {{
                        borderRadius: 8,
                        padding: 8,
                        marginTop: 8,
                        backgroundColor: "#F30"
                    }}
                    onPress = {this.clickJump.bind(this)}>
                    <Text>點擊進入第二個頁面</Text>
                </TouchableHighlight>
            </View>
        );
    }
}

 

//第二個頁面

class SecondPageComponent extends Component{
    clickJump(){
        const{navigator} = this.props;
        if(navigator){
            navigator.pop();  //navigator.pop 使用當前頁面出棧, 顯示上一個棧內頁面.
        }
    }

    render(){
        return(
            <View style = {styles.container}>
                <Text>我是第二個頁面</Text>
                <TouchableHighlight
                    underlayColor = "rgb(180, 135, 250)"
                    activeOpacity = {0.5}
                    style = {{
                        borderRadius: 8,
                        padding: 8,
                        marginTop: 5,
                        backgroundColor: "#F30"
                    }}
                    onPress = {this.clickJump.bind(this)}>
                    <Text>點擊返回第一個頁面</Text>
                </TouchableHighlight>
            </View>
        );
    }
}

 

const styles = StyleSheet.create({
 container: {
  flex: 1,
  justifyContent: 'center',
  alignItems: 'center',
  backgroundColor: '#FFFFFF',
 },
});

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

 

 

延伸:傳參。

以上面的代碼為基礎。

 

一:

效果圖:

 

//第一個界面
class FirstPageComponent extends Component{
    constructor(props){
        super(props);
        //參數來源
        this.state = {
            author: '華帥'
        };
    }

    clickJump(){
        const{navigator} = this.props;
        if(navigator){
            navigator.push({
                name: "SecondPageComponent",
                component: SecondPageComponent,
                //傳遞參數,入棧
                params: {
                    author: this.state.author,
                }
            });
        }
    }

    render(){
        return(
            <View style = {styles.container}>
                <Text>我是第一個頁面</Text>
                <TouchableHighlight
                    underlayColor = "rgb(180, 135, 250)"
                    activeOpacity = {0.5}
                    style = {{
                        borderRadius: 8,
                        padding: 8,
                        marginTop: 8,
                        backgroundColor: "#F30"
                    }}
                    onPress = {this.clickJump.bind(this)} >
                    <Text>點擊進入第二個頁面</Text>
                </TouchableHighlight>
            </View>
        );
    }
}

 

//第二個頁面
class SecondPageComponent extends Component{
    constructor(props) {
        super(props);
        this.state = {};
    }

    //接收傳遞過來的參數
    componentDidMount() {
        this.setState({
            author: this.props.author,
        });
    }

    clickJump(){
        const{navigator} = this.props;
        if(navigator){
            navigator.pop();
        }
    }

    render(){
        return(
            <View style = {styles.container}>
                <Text>我是第二個頁面</Text>
                <TouchableHighlight
                    underlayColor = "rgb(180, 135, 250)"
                    activeOpacity = {0.5}
                    style = {{
                        borderRadius: 8,
                        padding: 8,
                        marginTop: 5,
                        backgroundColor: "#F30"
                    }}
                    onPress = {this.clickJump.bind(this)}>
                    <Text>點擊返回第一個頁面</Text>
                </TouchableHighlight>
                <Text>作者是:{this.state.author}</Text>
            </View>
        );
    }
}                    

 

 

二:

效果圖:

 

//第一個頁面

class FirstPageComponent extends Component{

    constructor(props){
        super(props);
        //參數來源
        this.state = {
            author: "華帥",
            id: 1,
            user: null,
        };
    }

    clickJump(){
        const{navigator} = this.props;
        const self = this;
        if(navigator){
            navigator.push({
                name: "SecondPageComponent",
                component: SecondPageComponent,
                //傳遞參數,入棧
                params:{
                    author: this.state.author,
                    id: this.state.id,
                    //從第二頁獲取user
                    getUser: function(user){
                        self.setState({
                            user: user
                        });
                    }
                }
            });
        }
    } render(){
if(this.state.user){ return( <View> <Text style = {styles.container}>用戶信息:{JSON.stringify(this.state.user)}</Text> </View> ); }else{ return( <View style = {styles.container}> <Text>我是第一個頁面</Text> <TouchableHighlight underlayColor = "rgb(180, 135, 250)" activeOpacity = {0.5} style = {{ borderRadius: 8, padding: 8, marginTop: 8, backgroundColor: "#F30" }} onPress = {this.clickJump.bind(this)}> <Text>點擊進入第二個頁面</Text> </TouchableHighlight> </View> ); } } }

 

 

const USER_MODELS = {
    1: {name: '華帥', age: 44},
    2: {name: '小明', age: 12}
};

 

//第二個頁面

class SecondPageComponent extends Component{
    constructor(props){
        super(props);
        this.state = {
            id:null
        };
    }

    //接收傳遞過來的參數
    componentDidMount(){
        this.setState({
            author: this.props.author,
            id: this.props.id,
        });
    }

    clickJump(){
        const{navigator} = this.props;
        if(this.props.getUser){
            let user = USER_MODELS[this.props.id];
            this.props.getUser(user);
        }

        if(navigator){
            navigator.pop();
        }
    }

    render(){
        return(
            <View style = {styles.container}>
                <Text>我是第二個頁面</Text>
                <Text>ID: {this.state.id}</Text>
                <TouchableHighlight
                    underlayColor = "rgb(180, 135, 250)"
                    activeOpacity = {0.5}
                    style = {{
                        borderRadius: 8,
                        padding: 8,
                        marginTop: 5,
                        backgroundColor: "#F30"
                    }}
                    onPress = {this.clickJump.bind(this)}>
                    <Text>點擊返回第一個頁面</Text>
                </TouchableHighlight>
                <Text>作者是:{this.state.author}</Text>
            </View>
        );
    }
}

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM