前端技术前端知识

【React Native 极速指南】进阶篇

2019-04-27  本文已影响21人  一俢

这篇文章你将会学习到:

如何安装路由 react-navigation

如何使用路由

创建 StackNavigator

// home.js
import React from 'react';
import { View, Text } from 'react-native';

export default class HomeScreen extends React.Component {
    render() {
        return (
            <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
                <Text>Home Screen</Text>
            </View>
        );
    }
}
// navigator.js
import React from 'react';
import { createStackNavigator } from 'react-navigation';
import Home from './home';

const NavigationRouteConfigMap = {
    Home: {
        screen: Home,
        navigationOptions: {
            header: null,
            gesturesEnabled: false,
        },
    },
};

const StackNavigatorConfig = {
    initialRouteName: 'Home',
};

export default createStackNavigator(NavigationRouteConfigMap, StackNavigatorConfig);
// app.js
import React, { PureComponent } from 'react';
import AppNavigator from './navigator';

export default class App extends PureComponent {
    render() {
        return <AppNavigator />;
    }
}
// index.js
import {AppRegistry} from 'react-native';
import App from './src/app';
import {name as appName} from './app.json';

AppRegistry.registerComponent(appName, () => App);

页面间的转场和传递参数

// demo.js
import React from 'react';
import { View, Text } from 'react-native';

export default class DemoScreen extends React.Component {
    render() {
        return (
            <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
                <Text>Demo Screen</Text>
            </View>
        );
    }
}
// navigator.js
//...

const NavigationRouteConfigMap = {
    //...
    Demo: {
        screen: Demo,
        navigationOptions: {
            title: 'Demo'
        },
    },
};

//...
// home.js
import React from 'react';
import { View, Text, Button } from 'react-native';

export default class HomeScreen extends React.Component {
    render() {
        return (
            <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
                <Text>Home Screen</Text>
                <Button 
                    title="Go to Demo"
                    onPress={() => this.props.navigation.navigate('Demo', { test: 'hello' })}
                >
                </Button>
            </View>
        );
    }
}

相关配置

TODO

TabNavigator

TabNavigator

// tab.js
import React from 'react';
import { createBottomTabNavigator } from 'react-navigation';
import Home from './home';
import Demo2 from './demo2';

const NavigatorTab = {
    Home: {
        screen: Home,
        navigationOptions: {
            tabBarLabel: 'Home',
            showIcon: true,
        },
    },
    Demo2: {
        screen: Demo2,
        navigationOptions: {
            tabBarLabel: 'Demo',
            showIcon: true,
        },
    },
};

const TabOptions = {
    animationEnabled: true,
};

export default createBottomTabNavigator(NavigatorTab, TabOptions);
// navigator.js
// ...

const NavigationRouteConfigMap = {
    // ...
    Tab: {
        screen: Tab,
        navigationOptions: {
            header: null,
            gesturesEnabled: false,
        },
    }
};

const StackNavigatorConfig = {
    initialRouteName: 'Tab',
};

export default createStackNavigator(NavigationRouteConfigMap, StackNavigatorConfig);

其他通用组件

上一篇 下一篇

猜你喜欢

热点阅读