加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

react-native – 在React Native中使用ES6的getDefaultProps函数

发布时间:2020-12-15 20:16:25 所属栏目:百科 来源:网络整理
导读:我正在查看 DatePickerIOS组件的文档,他们使用getDefaultProps()来初始化组件中的props. getDefaultProps: function () { return { date: new Date(),timeZoneOffsetInHours: (-1) * (new Date()).getTimezoneOffset() / 60,}; },getInitialState: function(
我正在查看 DatePickerIOS组件的文档,他们使用getDefaultProps()来初始化组件中的props.

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
  };
}

我应该这样做.props = {}来设置我的默认道具?

解决方法

看到这个:

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 = {...}
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读