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

Can I use createAction with a static meta in a way that createReducer.handleAction allows? #234

Open
dimitropoulos opened this issue Jun 25, 2020 · 0 comments

Comments

@dimitropoulos
Copy link

I am looking at using redux-beacon to integrate analytics from the point of my redux store. To do this I need to add meta to some actions. So I basically have typical createAction that looks like this, let's say:

export const setExpertModeAction = createAction('config/SET_EXPERT_MODE')<boolean>();

I'm using this with createReducer like

export const configReducer = createReducer(initialState)
  .handleAction(setExpertModeAction, (state, action) => ({
    ...state,
    expertMode: action.payload
  });

And in my code I just have

dispatch(setExpertModeAction(true))

My problem is I want to, at the level of the creation of the action, create meta for the analytics.

I was able to mock this out as follows:

export interface ActionMetaType {
  analytics?: {
    eventName: string;
    otherEventAttr?: number;
  };
}

export const setExpertModeAction = <T extends boolean>(payload: T) => (
  createAction('config/SET_EXPERT_MODE')<T, ActionMetaType>()(
    payload,
    {
      analytics: {
        eventName: payload ? 'enabled expert mode' : 'disabled expert mode',
      },
    },
  )
);

That way, when I dispatch the action it doesn't need the meta to be there, just like before. My issue is, though, that when I run this I get:

Error: Argument 1 is invalid, it should be an action-creator instance from "typesafe-actions" or action type of type: string | symbol

Any suggestions?

@dimitropoulos dimitropoulos changed the title Can I use createAction with a static meta in a way that createReducer.handleAction allows? Can I use createAction with a static meta in a way that createReducer.handleAction allows? Jun 25, 2020
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