javascriptreactjsobservablekinvey

React - setState in an observable


I have an array

    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)
       });

Now in the onNext function, I would like to call the setState.

the array is empty after the observable and for some reason, the setState is not callable from inside the observable.


Solution

  • Following to a former question:

    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)
       });
    

    Use let that = this