Giter Site home page Giter Site logo

Comments (10)

murtraja avatar murtraja commented on May 3, 2024 5

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.

voodoods avatar voodoods commented on May 3, 2024 4

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.

pke avatar pke commented on May 3, 2024 3

Signatur of apply effect seems to have changed to apply(context, method...)

from redux-saga.

yelouafi avatar yelouafi commented on May 3, 2024

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.

tappleby avatar tappleby commented on May 3, 2024

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.

yelouafi avatar yelouafi commented on May 3, 2024

@tappleby I agree. Your solution looks better

from redux-saga.

yelouafi avatar yelouafi commented on May 3, 2024

FYI. the new v0.4.0 supports the apply effect.

from redux-saga.

inyono avatar inyono commented on May 3, 2024

Nice, thanks 👍

from redux-saga.

jgehrcke avatar jgehrcke commented on May 3, 2024

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.

jgehrcke avatar jgehrcke commented on May 3, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.