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

Question: Example of Nested Reducers w/ Split Actions #72

Open
jpgilchrist opened this issue Apr 27, 2018 · 2 comments
Open

Question: Example of Nested Reducers w/ Split Actions #72

jpgilchrist opened this issue Apr 27, 2018 · 2 comments

Comments

@jpgilchrist
Copy link

I was looking through the notes/documentation and I want to create a nested reducer for a complex application. I would like to have the actions split into separate, maintainable files for easier organization. I see in the docs for the NestedReducerBuilder you allude to BasActionNames and ChildActionNames, but I don't see how they all get connected together as your Store is created with something like

  final store = new Store<AppState, AppStateBuilder, AppActions>(
    appStateReducerBuilder.build(),
    new AppState(),
    new AppActions(),
    middleware: []
  );

which only includes the AppActions.

@davidmarne
Copy link
Contributor

Shoot, yea those docs are missing some context around the actions. In the example you are alluding to consider ChildActions and BaseActions to be defined as so:

// BaseActions is the main action class that contains
// all actions for your store. It is passed to the store at
// instantiation. It contains an action dispatcher and a reference
// to an instance of ChildActions.
abstract class BaseActions extends ReduxActions {
  BaseActions._();
  factory BaseActions() => new _$BaseActions();

  ActionDispatcher<Null> get baseAction;
  ChildActions get child;
}

// ChildActions contains an action dispatcher
abstract class ChildActions extends ReduxActions {
  ChildActions._();
  factory ChildActions() => new _$ChildActions();

  ActionDispatcher<Null> get childAction;
}

As you can see, ChildActions is actually a field on BaseActions, meaning those child actions can be dispatched given an instance of BaseActions itself.

final store = new Store<AppState, AppStateBuilder, AppActions>(
    appStateReducerBuilder.build(),
    new AppState(),
    new BaseActions(),
    middleware: []
  );

store.actions.baseAction();
store.actions.child.childAction();

The ActionNames classes are defined separately for BaseActions and ChildActions, and do not have references to each other at all.

@jpgilchrist
Copy link
Author

Interesting... so when you use ActionNames in your examples the code still knows how to access that name despite not having a relationship established?

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

2 participants