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

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

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

查看所有标签

猜你喜欢:

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

What Technology Wants

What Technology Wants

Kevin Kelly / Penguin Group (USA) Incorporated / 2010-10-14 / USD 27.95

A refreshing view of technology as a living force in the world. This provocative book introduces a brand-new view of technology. It suggests that technology as a whole is not a jumble of wires and ......一起来看看 《What Technology Wants》 这本书的介绍吧!

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

RGB HEX 互转工具

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

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

UNIX 时间戳转换