Javascript 反应-以可观察的方式设置状态
我有一个数组Javascript 反应-以可观察的方式设置状态,javascript,reactjs,observable,kinvey,Javascript,Reactjs,Observable,Kinvey,我有一个数组 let exams = [] and an observable stream.subscribe( function onNext(patients) { let patient = patients[0] await exams.push({ patient, session }) // this.s
let exams = []
and an observable
stream.subscribe(
function onNext(patients) {
let patient = patients[0]
await exams.push({
patient,
session
})
// this.setState(exams)
}, function onError(error) {
console.log('error',error)
}, function onComplete() {
console.log("exams",exams)
});
现在在onNext
函数中,我想调用setState
数组在可观察对象之后为空,并且由于某种原因,无法从可观察对象内部调用setState。以下是一个:
使用
let that=this
常规函数和箭头函数之间的关键字this
存在差异。大多数情况下,您需要在React中使用箭头函数,以便仍然可以访问组件的此
上下文
let that = this
stream.subscribe(
function onNext(patients) {
let patient = patients[0]
await exams.push({
patient,
session
})
that.setState({exams})
}, function onError(error) {
console.log('error',error)
}, function onComplete() {
console.log("exams",exams)
});