defaultProps in React Native?

后端 未结 3 2153
终归单人心
终归单人心 2021-01-06 01:02

Is it possible to use defaultProps in React Native? I’ve tried the following 2 ways of defining defaultProps and I get null when trying to access the defaultProp

<         


        
3条回答
  •  囚心锁ツ
    2021-01-06 01:38

    I do it in ReactNative

    import React, {
        Component
    } from 'react';
    import {
        View,
        Text
    } from 'react-native';
    import PropTypes from 'prop-types';
    
    export default class foo extends Component {
        static propTypes = {
            name: PropTypes.array,
            prop2: PropTypes.string
        }
        static defaultProps = {
            array: [1, 2, 4],
            name: 'abc'
        }
        constructor(props) {
            super(props);
            this.state = {};
        }
        render() {
            return (
                
                    Default Prop array is: {this.props.array}
                    Default Prop name is: {this.props.name}
                
            );
        }
    }
    
    

    And then you can use these props like this.props.prop1 and this.props.prop2.

提交回复
热议问题