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.页面变量的渲染和传递参数》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

Is Parallel Programming Hard, And, If So, What Can You Do About

Is Parallel Programming Hard, And, If So, What Can You Do About

Paul E. McKenney

The purpose of this book is to help you understand how to program shared-memory parallel machines without risking your sanity.1 By describing the algorithms and designs that have worked well in the pa......一起来看看 《Is Parallel Programming Hard, And, If So, What Can You Do About 》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

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

UNIX 时间戳转换

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具