Tuesday 1 November 2016

reactjs - setState with Firebase Promise in React

How can i set state inside react component?



I`ve got error message:




Uncaught TypeError: Cannot read property 'setState' of null




here is the component code listing:



class MessageList extends React.Component {
constructor(props){
super(props);
this.state = {
messages: []
};

var firebaseRef = firebase.database().ref();
firebaseRef.once('value')
.then(function(dataSnapshot) {
this.setState({
messages: messages
});
});
}

render() { ... }

}

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...