React-Navigation 初识导航器

文档属于翻译官网英文 文档。想要看原文,请https://reactnavigation.org/docs/intro/

# 1. 安装

NPM下安装
npm install --save react-navigation
Yarn安装
yarn add react-navigation

以下是nmp下创建一个项目,并且安装启动手机调试:


# Create a new React Native App
react-native init SimpleApp
cd SimpleApp

# Install the latest version of react-navigation from npm
npm install --save react-navigation

# Run the new app
react-native run-android # or:
react-native run-ios

当安装启动成功后。
页面会显示:welcome react-native

在当前目录下,创建app.js。并且用import './App';. 来导入它。同时将index.ios.js和 index.android.js里面的内容清除掉。显示内容放在app.js
这样就可以显示一个只有导入,导出的一个清爽首页了。


# 2. 引入堆栈导航器

迫不及待的想要体验堆栈导航器了吧,何不现在我们就开始!

1.在跟index.android.js同目录下创建app.js
2.在index.android.js中代码如下:


import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  Button
} from 'react-native';
import { StackNavigator } from 'react-navigation';
import { ChatScreen,HelloScreen } from './app';
class HomeScreen extends Component {
  static navigationOptions = {
    title: 'Home',
  };
  render() {
    const { navigate } = this.props.navigation;
    return (
      <View>
        <Text>Hello, Chat App!代码控制台</Text>
        <Button
          onPress={() => navigate('Chat')}
          title="Go Chat"
        />
      </View>
    );
  }
}
const SimpleApp = StackNavigator({
  Home: { screen: HomeScreen },
  Chat: { screen: ChatScreen },
});
AppRegistry.registerComponent('tang', () => SimpleApp);

# 3.app.js页面代码如下:

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  Button
} from 'react-native';
export  class ChatScreen extends Component {
  static navigationOptions = {
    title: 'Chat',
  };
  render() {
    return (
      <View>
        <Text>Chat with Lucy</Text>
      </View>
    );
  }
}

插入数据线,打开cmd命令进入应用所在根目录。
运行命令:
react-native run-android
等在手机安装完毕后,就可以看见运行结果了。

#4 .传递参数

场景:头部导航组件样式都一样,文字内容不一样,应该怎么实现。
要实现导航器之间的传递参数就应该在上一级页面导航器中定义 一个参数,然后在子页面或者下级页面接收完成传递。

1.我们更改index.android.js中的Button

<Button
          onPress={() => navigate('Chat', { user: 'Lucy' })}
          title="Chat with Lucy"
        />

通过navigate中传递一个数组来实现传值。

2.app.js中获取传值。

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

#5 .扩展阅读:

props(属性)
http://reactnative.cn/docs/0.47/props.html#content

state(状态)
http://reactnative.cn/docs/0.47/state.html#content

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,447评论 25 709
  • react-navigation导航组件使用详解 注意了,如果有小伙伴们发现运行作者提供的react-naviga...
    光强_上海阅读 23,558评论 38 103
  • 凡是经历过所谓的坎儿之后,大部分人都会想到逃离,可偏偏又身不由己,于是把自己想象成困在笼子里那般苦逼,所以更向往诗...
    周维格阅读 2,967评论 0 4
  • 一 概述 众所周知c语言是鼻祖.而让c语言的特点之一就是指针.那在Java中是没有指针这个概念的.但是没有并不表示...
    不解释sdx阅读 9,934评论 0 6
  • 将中中送上校车,想想不对,校车上怎么没写学校的名字,坐错校车了怎么办?昨晚让中中在他的笔记本上写家庭住址,他没写,...
    大红英阅读 1,748评论 3 3