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.

Optional `payload` in `ActionCreator`?

See original GitHub issue

Hello, 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:closed
  • Created 6 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
rclmenezescommented, Nov 27, 2017

Similarly, empty actions return undefined payloads:

import actionCreatorFactory from 'typescript-fsa';
const actionCreator = actionCreatorFactory();

const action = actionCreator('ACTION');
console.log(action())  // {type: 'ACTION', payload: undefined}

It’s possible for us to fix both by adopting the following syntax:

const emptyAction = actionCreator.empty('ACTION');
console.log(emptyAction())  // {type: 'ACTION'}

type Success = string;
type Error = string;
const asyncAction = actionCreator.emptyAsync<Success, Error>('ACTION');
console.log(asyncAction.started())  // {type: 'ACTION_STARTED'}
console.log(asyncAction.done('success'))  // {type: 'ACTION_DONE', payload: {results: 'success'}}
console.log(asyncAction.done('failure'))  // {type: 'ACTION_FAILED', payload: {results: 'failure'}}

Benefits of doing it this way:

Cons:

  • This would expand our supported API surface area
  • Async actions without parameters are pretty rare
  • Most people never notice that action.payload is undefined anyway 🤷‍♂️

If you decide that you like this, I’d be happy to make the PR 😃

1reaction
aikovencommented, Dec 14, 2017

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 for Promise do the same:

// in PromiseConstructor interface

new <T>(executor: (
  resolve: (value?: T | PromiseLike<T>) => void, 
  reject: (reason?: any) => void
) => void): Promise<T>;
Read more comments on GitHub >

github_iconTop 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 >

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