使用箭头功能时,this.setState不是函数

All*_*len 1 react-native

console.log打印出正确的数字,但this.setState引发错误.我以为我是使用箭头功能正确绑定它.

export default class App extends React.Component {

constructor(props) {
  super(props);
  this.emotion='happy';
  this.state = {
      idList:[]
  };
 }

componentWillMount() {
 this.getEmotionIDs();
}


getEmotionIDs = () => {
   firebase.database().ref(this.emotion).once('value').then(function(snapshot) {
      console.log("IDs: " + snapshot.val());
     this.setState({ idList: snapshot.val()});

  });
}   


render() { 

   return (
 ....
  )
Run Code Online (Sandbox Code Playgroud)

Rav*_*Raj 6

尝试使用箭头函数作为自动绑定的承诺

getEmotionIDs = () => {
   firebase.database().ref(this.emotion).once('value').then((snapshot) => {
      console.log("IDs: " + snapshot.val());
     this.setState({ idList: snapshot.val()});

  });
}   
Run Code Online (Sandbox Code Playgroud)