question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

identity function in createAction only handles the first parameter

See original GitHub issue

The createAction function returns an action creator with the signature function(...args). However, if you do not pass an actionCreator method to the createAction function, then the “identity” function is used.

The identity function is this: (t) => t, which does not do an argument spread and, since it is called with function.apply, it only handles the first argument passed in to the action creator, even though the action creator is designed around supporting n arguments.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:2
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
calvinnwqcommented, Jun 27, 2016

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.

1reaction
yangmillstheorycommented, Jun 24, 2016

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;)

👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

handleAction(s) - redux-actions
Wraps a reducer so that it only handles Flux Standard Actions of a certain type. ... argument ( reducer ) is undefined ,...
Read more >
Create Multiple Redux Actions with an Action Map in redux ...
In this lesson, we'll use the plural createActions function and an action map to create multiple action creators with a single utility from...
Read more >
Identity function tutorial in SQL Server
Let's insert a few records in this table and view the records. You can see the first employee gets an ID value 100...
Read more >
How to work with Roles in ASP.NET Core Identity - YogiHosting
NET Core Identity Role based Authentication; Download Source Codes ... The Create Action takes the Role name as a string in it's parameter ......
Read more >
NgRx: Action Creators redesigned - Medium
Good Action Hygiene helps a lot, but only if the application is already running and it ... The new function takes the initialState...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found