2016-05-12 78 views
0

我得到錯誤:陣營:默認道具出錯

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

我缺少什麼?謝謝。

+0

您是否將您的代碼轉換爲ES7? – madox2

+0

我這樣做,它工作'MeetingMap.defaultProps = {account:''}',位於類下面,然後導出。 –

回答

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; 
+0

謝謝你的努力。如果我使用0階段的ES7,我可以使用propTypes和defaultProps作爲類的靜態屬性? –

+1

是的。您可以直接使用預設或變換。 http://babeljs.io/docs/plugins/transform-class-properties/ – mgmcdermott

+0

但是,這仍然無法正常工作:http://jsfiddle.net/egs9L2r2/1/ – FDisk