2

I seem to be unable to use the this reference in a class with sagas / generator functions

export default class Foo {

  constructor(name) {
    this.name = name
  }

  *sayHello() {
    yield put('@@actions/say Hello', this.name)
  }  
}

// In a Saga
const foo = new Foo('World')
yield call(foo.sayHello) // Crashes as `this` becomes undefined

I found a small hack which is to pass the class itself as an argument so it could read the class variables, but it feels too hackish (and not thread safe but in my case it's not rly important)

// Use special argument which is the class itself
*sayHello(self) {
  yield put({type: '@@actions/SAY_HELLO', name: self.name})
}  

call(foo.sayHello, foo) // Works
Cyril Duchon-Doris
  • 12,964
  • 9
  • 77
  • 164

0 Answers0