Skip to content

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

@dimitropoulos

Description

@dimitropoulos

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions