1. 程式人生 > >React Native的props使用詳解

React Native的props使用詳解

static rip ng- ont nts -m https oot ner

普通傳參

傳遞
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使用詳解