Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the thunk params first and then spread other params #11

Open
emileber opened this issue Jan 16, 2019 · 0 comments
Open

Make the thunk params first and then spread other params #11

emileber opened this issue Jan 16, 2019 · 0 comments

Comments

@emileber
Copy link

emileber commented Jan 16, 2019

Disclaimer: I'm aware that this is not backward compatible.

Current behaviour

Right now, an action callback receives the thunk params last (source):

result = fn(...args, {getState, dispatch, extra});

Problem

It's harder to use optional params or just to validate them.

const myAction = createActionThunk('MY_ACTION', (a, b, { getState }) => {
  // do something with getState
  if (!b) {
    // do something or reject
  }
  return fetchData(a, b);
});

Then, calling myAction(a) will fail either because of the { getState } param destructuring, or because b is always present (as the thunk params object).

Proposition

Just place the thunk params object first, then spread the args.

result = fn({getState, dispatch, extra}, ...args);

Then, our action with optional params and/or validation will always work.

const myAction = createActionThunk('MY_ACTION', ({ getState }, a, b) => {
  // do something with getState
  if (!b) {
    // do something or reject
  }
  return fetchData(a, b);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant