Comments (10)
using arrow functions instead of methods also does the trick
for example:
convert this code
export default class API {
fetch() {
console.log(this);
}
}
to this
export default class API {
fetch = () => {
console.log(this);
}
}
from redux-saga.
In addition: Binding the class Object itself with yield call(MyClass.myMethod.bind(MyClass))
seems to preserve the this
context as well, even though apply
should be the solution of choice.
from redux-saga.
Signatur of apply
effect seems to have changed to apply(context, method...)
from redux-saga.
Seems a valid issue to me. yield call( backend.fetch.bind(backend) )
can not be tested easily, because the call backend.fetch.bind(backend)
will return a different result each time it's called.
Adding context (this) support to the call
effect is a bit awkward because the call(fn, ...args)
is variadic (I dont feel also the form call(null, fn, ...args)
for non-class functions)
I think we should provide another effect creator to handle this case like method(API, fetch, ...args)
from redux-saga.
Would it make sense to keep it similar to Function.prototype.call
and Function.prototype.apply
?
call
works as is, if you need binding use apply?
call(method, arg1, arg2, argN) // Could be an alias for apply(method, null, [arg1, arg2, argN])
apply(method, context, [arg1, arg2, argN])
from redux-saga.
@tappleby I agree. Your solution looks better
from redux-saga.
FYI. the new v0.4.0 supports the apply
effect.
from redux-saga.
Nice, thanks 👍
from redux-saga.
On the topic, from https://github.com/microsoft/TypeScript/wiki/'this'-in-TypeScript:
The biggest red flag you can keep in mind is the use of a class method without immediately invoking it. Any time you see a class method being referenced without being invoked as part of that same expression, this might be incorrect.
The same document lists the solution that @murtraja also mentioned: using an instance arrow function: https://github.com/microsoft/TypeScript/wiki/'this'-in-TypeScript#use-instance-functions
from redux-saga.
I like what's documented today, this signature of the call()
effect: call([context, fnName], ...args)
Same as call([context, fn], ...args) but supports passing a fn as string. Useful for invoking object's methods, i.e. yield call([localStorage, 'getItem'], 'redux-saga')
from redux-saga.
Related Issues (20)
- yield and takeLatest not working with jest HOT 8
- Waiting for an action with takeMaybe / take after END is dispatched for SSR HOT 7
- Is it possible to selectively cancel tasks in an actionChannel? Ie cancel the 3rd task out of 5 running ones. HOT 5
- Is it possible for a saga to "trace" the effect "chain"? HOT 4
- Delay inside of while loop may never fire with React Native 0.71.6 HOT 2
- UI freezes when chrome devtools is open HOT 4
- Redux 4.0 - Unable to access updated data using useSelector HOT 2
- could we add leading/trailing edge options for debounce? HOT 3
- Workflow has flaw
- Why not use the await and async instead of the generator and yield? HOT 1
- TS2345 error while putting thunk actions
- React native Redux Saga with Redux Tollkit
- Module '"redux-saga/effects"' has no exported member 'call'. HOT 4
- Is there a standard way to break while true loops with call effect when END is dispatched? HOT 1
- Can put type improvements be released downstream? HOT 2
- Sending very large files, tasks in parallel are using a lot of memory
- How to use package that use redux-saga as dependency when its in webpack externals? HOT 7
- Help me connect redux-saga with Nextjs 13.5 using app router HOT 2
- Update peer dependencies to include `redux@5` (currently beta) HOT 14
- feature request: interface for integration with other frameworks (like Vue) HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from redux-saga.