defaultProps in React Native?

后端 未结 3 2188
终归单人心
终归单人心 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:39

    You want this:

    Foo.propTypes = {
        someData: React.PropTypes.string
    };
    

    This says that someData should be a string, but it is not required.

    Then in your render(), you can do this:

    render() {
        return (
           
            {this.props.someData || Foo.defaultProps.someData} 
          
        );
      }
    

    This will use this.props.someData if it's provided or Foo.defaultProps.someData if not.

    I also think you need to wrap {this.props.someData || Foo.defaultProps.someData} in a Text element (i.e., {this.props.someData || Foo.defaultProps.someData}).

提交回复
热议问题