gpt4 book ai didi

react-native - 在 React Native 中使用 ES6 的 getDefaultProps 函数相当于什么?

转载 作者:行者123 更新时间:2023-12-03 15:30:13 30 4
gpt4 key购买 nike

我正在查看 的文档DatePickerIOS 组件,他们使用 getDefaultProps() 来初始化组件中的 Prop 。

  getDefaultProps: function () {
return {
date: new Date(),
timeZoneOffsetInHours: (-1) * (new Date()).getTimezoneOffset() / 60,
};
},

getInitialState: function() {
return {
date: this.props.date,
timeZoneOffsetInHours: this.props.timeZoneOffsetInHours,
};
},

使用 ES6 语法的等效项是什么?因为我一直在使用:
constructor(props) {
super(props);

this.state = {
//equivalent to getInitialState is here
};
}

我应该这样做 this.props = { } 来设置我的默认 Prop 吗?

最佳答案

看到这个:

https://github.com/facebook/react/issues/3725

class X extends React.Component {
}
X.defaultProps = {...}

如果你使用 babel-plugin-transform-class-properties 包,你可以这样做:
class X extends React.Component {
static defaultProps = {...}
}

关于react-native - 在 React Native 中使用 ES6 的 getDefaultProps 函数相当于什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39111134/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com