react native

react native navigation

2017-12-19  本文已影响0人  苍天霸气诀

官网地址:
https://reactnavigation.org/docs/navigators/custom

导航器跳转页面

译注:从0.44版本开始,Navigator被从react native的核心组件库中剥离到了一个名为react-native-deprecated-custom-components的单独模块中。如果你需要继续使用Navigator,则需要先npm i facebookarchive/react-native-custom-components安装,然后从这个模块中import,即import { Navigator } from 'react-native-deprecated-custom-components'.
github地址:https://github.com/wix/react-native-navigation

navigation的安装

npm install --save react-navigation

yarn add react-navigation

React Navigation默认的导航器:

创建导航器

import { StackNavigator } from 'react-navigation';

const RootNavigator = StackNavigator({

});

export default RootNavigator;

StackNavigator 基础用法/属性介绍

export  const SimpleApp = StackNavigator({
    Home: {screen: HomePage,
    },
    Chat: { screen: MyPage },
    CustomKeyPage: { screen: CustomKeyPage },


},{
    // 导航栏的显示模式, screen: 有渐变透明效果, float: 无透明效果, none: 隐藏导航栏
    headerMode:'none'

});

导航配置

导航视觉效果

  1. float:iOS默认的效果,可以看到一个明显的过渡动画。
  2. screen:滑动过程中,整个页面都会返回。
  3. none:没有动画。
  4. cardStyle:自定义设置跳转效果。

StackNavigator 简单例子 不传参

入口

import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import SimpleApp from './HomeScreen'


export default class App extends React.Component {
    render() {
        return <SimpleApp />;
    }
}

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

主页面:HomeScreen

import {
    AppRegistry,
    Text,
    View,
    Button,
    StyleSheet,
} from 'react-native';
import { StackNavigator } from 'react-navigation';
import React, {Component} from 'react';
import  ChatScreen from './ChatScreen'
class HomeScreen extends React.Component {
    static navigationOptions = {
        title: 'Welcome'
    };
    render() {
        const { navigate } = this.props.navigation;
        return(
            <View style={styles.container}>
                <Button
                    onPress={() => navigate('Chat')}
                    title="Chat with Lucy"
                />
            </View>
            )
    }
}
var styles = StyleSheet.create({
    container: {
        flex: 1,
    },
});
export  const SimpleApp = StackNavigator({
    Home: { screen: HomeScreen },
    Chat: { screen: ChatScreen },
});
module.exports = SimpleApp;

跳转的页面ChatScreen

import {
    AppRegistry,
    Text,
    View,
} from 'react-native';
import React, {Component} from 'react';
import { StackNavigator } from 'react-navigation';

class ChatScreen extends React.Component {
    // Nav options can be defined as a function of the screen's props:
    static navigationOptions = ({navigation}) => ({
        title: `Chat with`,
    });

    render() {
        // The screen's current route is passed in to `props.navigation.state`:
        // const {params} = this.props.navigation.state;
        return (
            <View>
                <Text>Chat with </Text>
            </View>
        );
    }
}
 module.exports = ChatScreen;
上一篇下一篇

猜你喜欢

热点阅读