Reactjs ReactFire:未捕获的语法错误:意外标记| componentWillMount
我试着从React开始,按照firebaseBlog上的例子,我试着运行下面的代码,我得到Reactjs ReactFire:未捕获的语法错误:意外标记| componentWillMount,reactjs,firebase,firebase-realtime-database,babeljs,reactfire,Reactjs,Firebase,Firebase Realtime Database,Babeljs,Reactfire,我试着从React开始,按照firebaseBlog上的例子,我试着运行下面的代码,我得到 Uncaught SyntaxError: http://localhost/h/js/test.js: Unexpected token (1:28) > 1 | componentWillMount: function() { | ^ 2 | this.firebaseRef = firebase.datab
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)
);
}
如果您的组件声明扩展了React.component,就像这样
class TestComponent extends React.Component {
...
}
那么您的组件willmount()和其他函数应该是这样的
componentWillMount() {
...
}
firebaseBlog上的示例
,请更具体一点,什么示例?此代码段是有效react组件的一部分,但您仍然需要组件声明的其余部分。