@ngrx/store action undefined when dispatched

76 Views Asked by At

I'm playing around with angular and ngrx/store 17.

I set up some actions, defined the relatives reducers and dispatched the action, but this error is showing up:

TypeError: can't access property "type", creator is undefined

It happens inside function on() when it tries to extract the action's type and the cause seems to be the action being undefined.

I did this things so many time I can't count, but something like this never happened to me.

This is the definition of my action

export const setTitle = createAction(`Set title`, props<{ title: string }>());

the reducer

export const sharedFeature = createFeature({
    name: sharedFeatureKey,
    reducer: createReducer(
        initialState,
        on(SharedActions.setTitle, (state, { title }) => ({ ...state, title })),
    ),
});

and the registration in the module:

StoreModule.forFeature(fromShared.sharedFeature),

I can't really understand what I'm doing wrong and I can't find anyting on the internet.

EDIT:

After some experiments I found out that the cause seems to be related to the fact that the module is not tied to any route.

Defining a route for the module within the app.module solve the problem, but I already did something similar before (in Angular 8) and didn't have this kind of problem.

Did something changed with the latest versions?

EDIT2:

It's not the route, it happens when I try to dispatch an action from inside a component which reside in an other module. But this doesn't explain why dispatch inside the module still return the error if the module is not tied to a route.

2

There are 2 best solutions below

0
Federico Xella On BEST ANSWER

As last resort I tried to create an other project excluding the standalone API (--standalone false) and this seems to have solved the issue: now I can dispatch actions relative to a store which is in an imported Module.

Maybe standalone API and StoreModule.forFeature shouldn't be used together.

1
timdeschryver On

It's probably because the creator's method is not invoked while dispatching it?

I expect to see (and feel free to update the question if this is not the case):

this.store.dispatch(setTitle)

This should become:

this.store.dispatch({title: ''})

If that's not the case a reproduction will be useful. Another thing that could be the cause is how SharedActions is imported.