2016-11-14 96 views
0
<script type="text/babel"> 
    class List extends React.Component { 
     state = { 
      objects: ['1', '2', '3'] 
     }; 
     render() { 
      return(<div> objects[1] </div>) 
     } 
    } 

錯誤:browser.min.js:41未捕獲的語法錯誤:嵌入式:意外的令牌(4:18)嵌入式:意外的令牌。反應。

2 |   class List extends React.Component { 
    3 | 
> 4 |    state = { 
    |     ^
    5 |     objects: ['1', '2', '3'] 
    6 |    }; 

我無法理解((

+0

你不能聲明實例變量直接在es6中。請參閱鏈接http://stackoverflow.com/questions/22528967/es6-class-variable-alternatives?answertab=votes#tab-top – vijay

回答

0
<script type="text/babel"> 
    class List extends React.Component { 
     constructor(props) { 
      this.state = { 
       objects: ['1', '2', '3'] 
      }; 
     } 
     render() { 
      return(<div> {this.state.objects[1]} </div>) 
     } 
    }