Git Product home page Git Product logo

Comments (11)

yangmillstheory avatar yangmillstheory commented on July 17, 2024 1

TL; DR I'm not sure there's an issue here.

from redux-actions.

killermonk avatar killermonk commented on July 17, 2024 1

Easy fix: function(...args) { return args.length === 1 ? args[0] : args; }

If I pass one argument, I'd just expect that argument. If I pass multiple arguments I'd expect an array.

In my use, though, if I'm passing arguments to the action, I also use a custom action creator with it. They are generally in the form of (arg1, arg2) => ({arg1, arg2}), so that I get a single object with the key being the name of the argument.

All in all, if you disagree with my opinion on how an identity should work, it's worth it just to close the ticket. This mostly just threw me off when I first started using this library.

from redux-actions.

yangmillstheory avatar yangmillstheory commented on July 17, 2024 1

Yeah, I'm wondering what the maintainers or the upvoters think. ping @timche @hale @calvinnwq.

I didn't mean to shoot your point down; after discussion what you propose seems reasonable! I do think that in the case multiple arguments, someone might be passing a custom action creator. But this would handle the case of having to pass

(...args) => (args.length === 1 ? args[0] : args;)

👍

from redux-actions.

calvinnwq avatar calvinnwq commented on July 17, 2024 1

I personally do (arg1, arg2) => ({ arg1, arg2 }) if i have arguments to pass.
Even if it were a single argument, I'd still do (arg1) => ({ arg1 }) just to keep it consistent and make it more readable in the reducers, especially in larger apps.

e.g.

// action
const actions = {
  entry: {
    add: createAction('ADD_ENTRY', (entry) => ({ entry })),
  }
};
// reducer
[actions.entry.add]: (state, payload) => ({
    ...state,
    entry: [
      ...state.entry,
      payload.entry,
    ],
  }),

Since I pass my own payloadCreator all the time, I don't encounter the problem as discussed here.

Also, since JavaScript does not have support for named parameters like in some other languages e.g. python where you can specify functionName(arg1=1, arg2=2), you can pass an object literal as your only argument.

const addEntry = createAction('ADD_ENTRY'),
const entry = { name: 'entry name' };
addEntry({ entry }); // same as addEntry({ entry: entry });

this then will work with redux-actions with the default identity


I don't really agree with passing multiple arguments as an array as payload, since you'd have to again assume the order of the arguments passed which makes code less readable and less flexible.

But, not passing all arguments is bad as well as it restricts by default, so I'd say the solution mentioned by @yangmillstheory above is sufficient.

from redux-actions.

yangmillstheory avatar yangmillstheory commented on July 17, 2024

What should the behavior be for the default function when N arguments are passed?

The returned actionHandler is responsible for building an FSA (relevant code here) - it seems like the only reasonable thing to do when N arguments are passed is to return the first argument?

If you're calling an action creator with N arguments, it seems like that signature would imply/necessitate a custom action creator.

from redux-actions.

killermonk avatar killermonk commented on July 17, 2024

Sorry for my delay. I guess what I really want is something like this for identity:
function(...arguments) { return arguments; }

It just seemed off to me when reading through to code to see something allowing for n arguments, then ignoring them by default instead of just returning back those arguments.

from redux-actions.

yangmillstheory avatar yangmillstheory commented on July 17, 2024

Hm interesting.

But in the case of one argument 'arg', would expect action.payload to be ['arg']?

from redux-actions.

yangmillstheory avatar yangmillstheory commented on July 17, 2024

@calvinnwq I like the pattern of named multiple arguments using a POJO. Definitely readable and more intentional even in the case of one argument.

from redux-actions.

timche avatar timche commented on July 17, 2024

Don't forget, payload can be any type of value. Whenever I need "to pass more than 1 argument", I also use an object instead, so it is clear what the reducer will receive and we have "named" values.

@killermonk @yangmillstheory: Are you okay with that approach?

from redux-actions.

yangmillstheory avatar yangmillstheory commented on July 17, 2024

@timche I'm OK with this approach (encouraging named parameters if I understand correctly), and it's something I use at work. Having named arguments makes the code more intentional/readable I think.

Come to think of it, I haven't found great use cases for payload creators yet (I just use the identity with plain object payloads), and separately am interested to hear of any.

from redux-actions.

yangmillstheory avatar yangmillstheory commented on July 17, 2024

Closing this as there's a good and readable alternative (using a plain JS object).

from redux-actions.

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.