2017-02-18 63 views
0

我想開始反應,下面就firebaseBlog的例子中,我試圖運行下面的代碼片段,我明白,ReactFire:未捕獲的SyntaxError:意外的令牌| componentWillMount

Uncaught SyntaxError: http://localhost/h/js/test.js: Unexpected token (1:28) 
> 1 | componentWillMount: function() { 
    |       ^
    2 |   this.firebaseRef = firebase.database().ref("items"); 
    3 |   this.firebaseRef.on("child_added", function(dataSnapshot) { 
    4 |    this.items.push(dataSnapshot.val()); 

和代碼被稱爲是這樣的。

componentWillMount: function() { 
     this.firebaseRef = firebase.database().ref("items"); 
     this.firebaseRef.on("child_added", function(dataSnapshot) { 
       this.items.push(dataSnapshot.val()); 
       this.setState({items: this.items}); 
      }.bind(this) 
     ); 
    } 
+1

'firebaseBlog'上的例子,請更具體一些,舉個例子?這個片段是一個有效的反應組件,但你仍然需要組件聲明的其餘部分。 – loganfsmyth

回答

0

如果你的組件聲明延伸React.Component,像這樣......

class TestComponent extends React.Component { 
    ... 
} 

那麼你componentWillMount()等功能應該是這樣的......

componentWillMount() { 
    ... 
} 
相關問題