Comments (11)
TL; DR I'm not sure there's an issue here.
from redux-actions.
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.
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.
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.
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.
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.
Hm interesting.
But in the case of one argument 'arg'
, would expect action.payload
to be ['arg']
?
from redux-actions.
@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.
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.
@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.
Closing this as there's a good and readable alternative (using a plain JS object).
from redux-actions.
Related Issues (20)
- [Feature request] Function value for defaultState HOT 1
- it's support immutable.js?
- can't use Map on handleActions HOT 2
- combine-actions prefixing bug
- Error handling on action payload HOT 2
- Docs: broken link
- Suddenly can't add an action... HOT 3
- How to use react hooks aftrer reducer change for the lib?
- Does state changing is async? HOT 3
- Looking for Maintainers
- React SSR handleActions cache bug HOT 1
- handleActions with Immer. HOT 1
- Wierd behavior with just not recognizing string templated reducer HOT 1
- 'to-camel-case' pacakge introducing bug since it's not compatible with 'lodash.camelCase' HOT 1
- v3.0.0 + TypeScript Cannot find module 'redux-actions' from ... HOT 12
- Import fails to identify entry point HOT 2
- can redux-actions be used with redux-thunk?
- Not Working with React Native from v.3.0.0
- Compilation error in `react-native:0.71.8` HOT 1
- Not working with `Jest` from v3.0.0 HOT 1
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-actions.