在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)
尝试使用箭头函数作为自动绑定的承诺
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)
| 归档时间: |
|
| 查看次数: |
649 次 |
| 最近记录: |