0
SETSTATEを使用しようとすると、私は次のエラーを取得する
を取得し、動作していない反応します。この文脈はもうコンポーネントではないので、これが起こっているはSETSTATEがエラー
export default React.createClass({
getInitialState: function() {
return {
name: '',
address: '',
phone: '',
email: '',
}
},
componentWillMount(){
this.getUserDetails();
},
getUserDetails: function(){
var personID = 'ABC123';
if(personID.length > 0){
new Firebase('https://xxxxxxxxxx.firebaseio.com/users/' + personID).on('value', function(snap) {
this.setState({
name: snap.val().Name + " " +snap.val().Surname
});
});
}
},
render() {
return (...);
}
});
Firebaseコールバックに「this」をバインドするか、代わりに**矢印機能**を使用する必要があります。 .on( 'value'、function(snap){...} .bind(this) ' –
または[arrow functions](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference)を使用してください。/Functions/Arrow_functions)、レキシカルスコープに依存する – noahnu