ReactNative中Props

栏目: IT技术 · 发布时间: 5年前

内容简介:props是属性,用来描述组件的特征,是由父组件传递给子组件的。Mine.js是父组件,传递name给子组件MineView.js是子组件

一、什么是props

props是属性,用来描述组件的特征,是由父组件传递给子组件的。

二、如何使用props

Mine.js是父组件,传递name给子组件

import React, { Component } from 'react';
import{View}from 'react-native';
import MineView from './MineView';
export default class Mine extends Componment{
  render(){
    return (<View>
        <MineView 
            name = "我的"
        />
    </View>)
  }
}

MineView.js是子组件

import React, { Component } from 'react';
import{Text}from 'react-native';
export default class MineView extends Componment{
  render(){
    return (<Text>Hello,{this.props.name}</Text>)
  }
}

三、什么是默认属性以及它的作用

通过defaultProps定义默认属性,如果上个页面没有传name的话,也能显示默认的值。

static defaultProps={
  name:'首页'
}

四、如何对props进行检查

为了保证上个页面传的属性的正确性,可以PropTypes通过对属性进行类型检查。

使用时需要导入PropTypes

import React,{Component,PropTypes} from 'react'

static propTypes={
  name:PropTypes.string,
}

如何上个页面传的是number类型的话,就会产生类型检查的警告Waring:Failed prop type:Invalid prop ‘name’ of type ‘number’ supplied to ‘PropsTest’.


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

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

PHP Hacks

PHP Hacks

Jack Herrington D. / O'Reilly Media / 2005-12-19 / USD 29.95

Programmers love its flexibility and speed; designers love its accessibility and convenience. When it comes to creating web sites, the PHP scripting language is truly a red-hot property. In fact, PH......一起来看看 《PHP Hacks》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

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

UNIX 时间戳转换

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试