ReactFire : Uncaught SyntaxError: Unexpected token | componentWillMount

362 Views Asked by At

I'm trying to start with React, following the example on firebaseBlog, I tried running the following piece of code, I get,

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());

And the code being referred to is this.

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

There are 1 best solutions below

0
On

If your component declaration extends React.Component, like so ...

class TestComponent extends React.Component {
  ...
}

then your componentWillMount(), and other functions should be like this ...

componentWillMount() {
  ...
}