内容简介: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’.
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Practical Algorithms for Programmers
Andrew Binstock、John Rex / Addison-Wesley Professional / 1995-06-29 / USD 39.99
Most algorithm books today are either academic textbooks or rehashes of the same tired set of algorithms. Practical Algorithms for Programmers is the first book to give complete code implementations o......一起来看看 《Practical Algorithms for Programmers》 这本书的介绍吧!