React Native 中的 defaultProps?

编程入门 行业动态 更新时间:2024-10-07 22:29:54
本文介绍了React Native 中的 defaultProps?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是否可以在 React Native 中使用 defaultProps?我尝试了以下 2 种定义 defaultProps 的方法,但在尝试访问 defaultProp 时得到 null

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

export default class Foo extends Component { // 1. define defaultProps within Class static defaultProps = { someData: 'Hello', } constructor(props) { super(props); } componentDidMount() { console.log(this.props.someData); } render() { return ( <View> {this.props.someData} </View> ) } } // 2. define defaultProps outside Class Foo.defaultProps = { someData: 'Hello' }

推荐答案

我总是这样做,而且效果很好:

I always do it like this and it works just fine:

class Foo extends React.Component {}; Foo.propTypes = { animateBackground: PropTypes.bool }; Foo.defaultProps = { animateBackground: false }; export default Foo;

更多推荐

React Native 中的 defaultProps?

本文发布于:2023-11-27 21:29:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639672.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:React   Native   defaultProps

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!