我想从火力传递returend参数的状态PARAM,但我发现这个错误的特性“的setState”:阵营+火力地堡:类型错误:无法读取空
App.js:43 Uncaught TypeError: Cannot read property 'setState' of null
我想将“快照”数组传递到“日历”,然后检索日历值,如“calendars.summary”或“calendars.date”(我知道如何做,但问题是我无法将快照分配给日历)。
componentDidMount() {
//setInterval(() => start(this.state.gapi,true), 30000);
var rootRef = firebase.database().ref();
var urlRef = rootRef.child("testCalendar/calendars");
urlRef.once("value", function(snapshot) {
snapshot.forEach(function(child) {
console.log(child.key+": "+child.child("summary").val());
});
this.setState({
calendars: snapshot
});
});
}
我该如何解决?由于
UPDATE Mayank Shukla'comment后,我能够做正确传递 “快照”:
componentDidMount() {
//setInterval(() => start(this.state.gapi,true), 30000);
var rootRef = firebase.database().ref();
var urlRef = rootRef.child("testCalendar/calendars");
urlRef.once("value", function(snapshot) {
snapshot.forEach(function(child) {
console.log(child.key+": "+child.child("summary").val());
});
this.setState({
calendars: snapshot
});
}.bind(this));
}
或
componentDidMount() {
//setInterval(() => start(this.state.gapi,true), 30000);
var rootRef = firebase.database().ref();
var urlRef = rootRef.child("testCalendar/calendars");
urlRef.once("value", (snapshot) => {
snapshot.forEach(function(child) {
console.log(child.key+": "+child.child("summary").val());
});
this.setState({
calendars: snapshot
});
});
}
但现在我有这样的问题:
关于此功能:
_getCalendarsList(){
var d = new Date();
return this.state.calendars.map((calend) => {
return (
<Calendar
{...calend}
key={d.getTime()} />
)
});
}
这是一个绑定问题,使用箭头函数:'urlRef.once(“value”,(snapshot)=> {'解决问题。 –
可能重复的[不能setState内部回调](http://stackoverflow.com/questions/43275845/cannot-setstate-inside-callback) –
@MayankShukla确定它的工作原理,但现在我有这个问题:“TypeError:this.state .calendars.map不是一个函数“快照可能不是一个迭代? –