React - setState in an observable

417 Views Asked by At

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.

1

There are 1 best solutions below

0
On BEST ANSWER

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