Skip to content Skip to sidebar Skip to footer

Reference To "this" Is Null In Function Called By Redux-saga Call

I'm learning redux-saga and I'm trying to integrate it into a project that uses an API that is generated with openapi-generator which produces output like the following: async logi

Solution 1:

Context in Javascript is dynamic based on the way you've written your code

const loginUser = User.loginUser
loginUser() // context lost, because there is no longer `User.` in front of it

Same applies when you pass a function as an parameter. This means that you have to provide the call effect context in some way. The bind method is one option, however the effect itself supports multiple ways of providing context. You can find them in the official docs https://redux-saga.js.org/docs/api/#callcontext-fn-args, here is the short version of it:

Possible ways to pass context:

call([context, fn], ...args)
call([context, fnName], ...args)
call({context, fn}, ...args)

So for example you can do the following:

const response = yieldcall([User, User.loginUser], action);

Post a Comment for "Reference To "this" Is Null In Function Called By Redux-saga Call"