react - 3.页面变量的渲染和传递参数

栏目: IOS · Android · 发布时间: 7年前

内容简介:参考:1. 修改Home.js文件,用来产生跳转2. 为这个 "SayHi" 页面创建文件:( ./screens/SayHi.js)

普通变量的渲染

render(){
    const message = "明日ReactNative 大神就要诞生了!"
    return (
      <View>
        <Text>
          {message}
        </Text>
      </View>
   )
  }

跳转页面时,传递参数

参考: https://reactnavigation.org/docs/en/params.html

1. 修改Home.js文件,用来产生跳转

// 在  Home.js 文件中, 增加该代码: 
        <Button title = "传递参数到新页面: (打个招呼) "
          onPress = {
            () => this.props.navigation.navigate("SayHi", { name: 'Siwei'})
          }
        />

2. 为这个 "SayHi" 页面创建文件:( ./screens/SayHi.js)

import React,  { Component} from 'react';
import {Platform, StyleSheet, Text, View, Button} from 'react-native'

export default class AboutUs extends React.Component{

  render(){
    const name = this.props.navigation.getParam('name', "Lily")
    return (
      <View>
        <Text>
        你好啊, {name }
        </Text>
      </View>
    )
  }

}

3. 添加对应的路由: (修改App.js)

import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View, Button, Alert} from 'react-native';
import { createStackNavigator, createAppContainer } from 'react-navigation';

import Home from 'screens/Home'
import AboutUs from 'screens/AboutUs'
// 新引入该文件
import SayHi from 'screens/SayHi'

const myNavigator = createStackNavigator(
  {
    Home: Home,
    AboutUs: AboutUs,
    // 在路由中做标记, 不加这一行的话,页面不会跳转。 
    SayHi: SayHi
  },
  {
    initialRouteName: "Home"
  }
)

export default createAppContainer(myNavigator)

这样, 点击Home页的按钮,就可以看到效果了 , 页面显示 hello siwei


以上所述就是小编给大家介绍的《react - 3.页面变量的渲染和传递参数》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Artificial Intelligence

Artificial Intelligence

Stuart Russell、Peter Norvig / Pearson / 2009-12-11 / USD 195.00

The long-anticipated revision of this #1 selling book offers the most comprehensive, state of the art introduction to the theory and practice of artificial intelligence for modern applications. Intell......一起来看看 《Artificial Intelligence》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

URL 编码/解码
URL 编码/解码

URL 编码/解码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换