码迷,mamicode.com
首页 > 其他好文 > 详细

React Native的props使用详解

时间:2017-11-26 22:58:45      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:cto   类型检查   赋值   默认   使用详解   小米   https   types   test   

普通传参

传递
export default class App extends Component<> {

    constructor() {
        super();
    }

    render() {
        return (
            <View style={styles.container}>
                <PropTest name = "呵呵"/>
            </View>
        );
    }
}
接收
export default class PropTest extends Component {

    render() {
        return <Text style={{fontSize: 28, color: ‘red‘}}>姓名:{this.props.name}</Text>
    }
}

设置默认的参数

子类组件
export default class PropTest extends Component {
    static defaultProps = {
        name: "小猪"
    }

    render() {
        return <Text style={{fontSize: 28, color: ‘red‘}}>姓名:{this.props.name}</Text>
    }
}
调用者
export default class App extends Component<> {

    constructor() {
        super();
    }

    render() {
        return (
            <View style={styles.container}>
                <PropTest/>
            </View>
        );
    }
}

参数的类型检查

https://react.bootcss.com/react/docs/typechecking-with-proptypes.html

{…object}ES6语法延展操作符

调用者
import React, {Component} from ‘react‘;
import { StyleSheet, View} from ‘react-native‘;
import PropTest from "./PropTest";

export default class App extends Component<> {

    constructor() {
        super();
    }

    render() {
        var props = {name: "小米", age: 18, sex: "男"}
        return (
            <View style={styles.container}>
                <PropTest {…props}/>
            </View>
        );
    }
}
import React from ‘react‘;
import {Text, View} from "react-native";


export default class PropTest extends React.Component {

    render() {

        return (
            <View>
                <Text style={{fontSize: 28, color: ‘red‘}}>姓名:{this.props.name}</Text>
                <Text style={{fontSize: 28, color: ‘red‘}}>年龄:{this.props.age}</Text>
                <Text style={{fontSize: 28, color: ‘red‘}}>年龄:{this.props.sex}</Text>
            </View>
        );
    }
}

解构赋值

调用者
import React, {Component} from ‘react‘;
import {StyleSheet, View} from ‘react-native‘;
import PropTest from "./PropTest";

export default class App extends Component<> {

    constructor() {
        super();
    }

    render() {
        var props = {name: "小米", age: 18, sex: "男"};
        var {name, sex} = props;

        return (
            <View style={styles.container}>
                <PropTest name={name} sex={sex}/>
            </View>
        );
    }
}
import React from ‘react‘;
import {Text, View} from "react-native";


export default class PropTest extends React.Component {


    static defaultProps = {
        age: 18
    }


    render() {

        return (
            <View>
                <Text style={{fontSize: 28, color: ‘red‘}}>姓名:{this.props.name}</Text>
                <Text style={{fontSize: 28, color: ‘red‘}}>年龄:{this.props.age}</Text>
                <Text style={{fontSize: 28, color: ‘red‘}}>年龄:{this.props.sex}</Text>
            </View>
        );
    }
}

React Native的props使用详解

标签:cto   类型检查   赋值   默认   使用详解   小米   https   types   test   

原文地址:http://www.cnblogs.com/zsjblog/p/7900716.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!