0

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.

Negin msr
  • 113
  • 9
Subhi Samara
  • 77
  • 1
  • 2
  • 11
  • There's a difference in the keyword `this` between regular functions and arrow functions. Most of the time you'll want arrow functions in React so you can still access the component's `this` context – Jayce444 Oct 05 '20 at 08:57
  • 1
    https://stackoverflow.com/a/35020509/5734311 –  Oct 05 '20 at 08:58

1 Answers1

0

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

Subhi Samara
  • 77
  • 1
  • 2
  • 11