Skip to content

Access slice of state in feature module #496

Closed
@doender

Description

@doender

[x] Documentation issue or request

Given the following in app.module.ts (and assuming we cannot change the state's structure):

StoreModule.forRoot({user: userReducer});

And having the following in a feature module:

StoreModule.forFeature('feature', featureReducer)

What is the most idiomatic way to access (read-only) user in featureReducer?

The solution I came up with is dispatch another action that adds user so that it can be used in the featureReducer:

@Effect()
augmentAction$ = this.action$
    .ofType(Actions.Action1)
    .withLatestFrom(this.store$)
    .switchMap(([action, state]:[Action, AppState]) => {
        return [new Actions.Action2(state.user)];
    });

This works, but it becomes hard to manage if almost every action needs to be transformed into another action if user is needed often. Is there a better way to handle this?

Other things I've thought about doing:

  • Add the user in the action creator (is this possible? desirable?)
  • Combine the reducers in such a way that user is directly available in featureReducer

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions