React Native 中的 defaultProps? [英] defaultProps in React Native?
本文介绍了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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文