React:默认道具错误

时间:2016-05-12 17:19:49

标签: reactjs

我收到错误:

Unexpected token (14:21)
  12 | class MeetingMap extends React.Component {
  13 |
> 14 |     static propTypes = {
     |                      ^
  15 |         accountId: PropTypes.string.isRequired
  16 |     };
  17 |

这是代码:

static propTypes = {
    account: PropTypes.string.isRequired
};

static defaultProps = {
    account: ''
};

我错过了什么?谢谢。

1 个答案:

答案 0 :(得分:2)

正如您在与您的问题相关的this GitHub issue中所看到的,类属性是ES7的第0阶段提案。如果您想使用此功能,则需要Babel stage 0 preset。否则,您需要使用添加propTypes的默认方法,如React docs中所示。

import React, { Component, PropTypes } from 'react';

class Counter extends Component {
  constructor(props) {
    super(props);
    this.state = {count: props.initialCount};
    this.tick = this.tick.bind(this);
  }
  tick() {
    this.setState({count: this.state.count + 1});
  }
  render() {
    return (
      <div onClick={this.tick}>
        Clicks: {this.state.count}
      </div>
    );
  }
}

Counter.propTypes = { initialCount: PropTypes.number };
Counter.defaultProps = { initialCount: 0 };

export default Counter;