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

createReducer is missing metadata #13

Open
sregg opened this issue Oct 1, 2019 · 3 comments
Open

createReducer is missing metadata #13

sregg opened this issue Oct 1, 2019 · 3 comments

Comments

@sregg
Copy link

sregg commented Oct 1, 2019

First of all, congrats for the new createReducer. This is super helpful as we don't have to define types anymore!

The only issue I'm seeing is that I can't seem to grab the metadata, only the payload in fulfilled state

@omichelsen
Copy link
Owner

Could you give an example where you need it, just so I am sure I fully understand the use case?

@sregg
Copy link
Author

sregg commented Apr 7, 2020

For instance if I have a list of items, I'd like to differentiate the initial API call (loading indicator centered) from the pull-to-refresh (loading indicator at the top) so I'm using a flag isPullToRefresh in my metadata. Then based on that metadata I'm setting my state isLoading or isPullRefreshing.

  switch (action.type) {
    case String(actionLoadResidentBalances.pending):
      const isPullRefreshing = action.meta?.isPullRefreshing ?? false;
      return {
        ...state,
        isLoadingCurrentBalances: !isPullRefreshing,
        isRefreshingCurrentBalances: isPullRefreshing,
      };

@stephenhand
Copy link

To elaborate - in my experimentation, the meta data is present on the async actions. It just isn't present on the type inferred for each of the async actions.

If you cast the type back to the original action type, you can access the meta property and the data is present as expected, e.g.

export const asyncAction = createAsyncAction(
  'myAction', 
  () => Promise.resolve('a payload'), 
  () => ({ someValue: 'something'}) 
);

export const reducer = createReducer(initialState, handleAction => [
  handleAction(asyncAction.pending as typeof asyncAction, (state, action) => {
    return {
      ...state,
      someProperty: action.meta.someValue
  }),
]);

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

3 participants