Optional `payload` in `ActionCreator`?
See original GitHub issueHello, thanks for the library!
I’ve got a question regarding async
action creator. What if my action should receive no params?
export const FETCH_TEAMS = 'FETCH_TEAMS';
const fetchTeams = actionCreator.async<
{}, // <- here, how can we specify to be optional?
Array<Team>,
AxiosError
>(FETCH_TEAMS);
// how i can handle it how
fetchTeams.started({});
fetchTeams.done({
result: teams,
params: {}
});
// how it would be nice to handle, but now i see errors in this case
fetchTeams.started();
fetchTeams.done({
result: teams
});
Thanks in advance!
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
createAction - Redux Toolkit
To do this, createAction accepts an optional second argument: a "prepare callback" that will be used to construct the payload value. TypeScript ...
Read more >redux action type has optional payload property-typescript ...
I have optional payload property, because sometimes I don't pass a payload to the reducer. However, because of this, inside the reducer, ...
Read more >Action Creators - Human Redux
It's just a utility that takes an action creator (or a whole object of action creators) and binds them all to the dispatch...
Read more >Advanced Actions in Redux - DEV Community
Action creator encapsulates an action and optional payload in a reusable pure function. function shutWindows(reason, ...id) { return { type: ...
Read more >createAction.ts - UNPKG - @reduxjs/toolkit
118, * An action creator of type `T` that takes an optional payload of type `P`. 119, *. 120, * @inheritdoc {redux#ActionCreator}.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Similarly, empty actions return
undefined
payloads:It’s possible for us to fix both by adopting the following syntax:
Benefits of doing it this way:
asyncAction.started({})
Cons:
action.payload
is undefined anyway 🤷♂️If you decide that you like this, I’d be happy to make the PR 😃
I would rather not expand the API because of TS shortcomings.
The
undefined
payload can be easily fixed at runtime, not the typing side.I’m thinking about just making a
payload
parameter optional This would make it a bit less type-safe, but at least it would get easier to express things. For instance, the built-in typings forPromise
do the same: