Skip to content

Is there a better way to handle nested properties in state? #37

@AxelMarquez

Description

@AxelMarquez

For example, I have the following nested state (only left the relevant parts for clarity):

class AppState {

   final OpenBook openBook;
}

class OpenBook {

  final bool headerIsFetching;
}

 
And the reducer:

new AppState(

  openBook: new OpenBook(//<- This can be optimized! new instance is built for every cycle regardless of the action
    headerIsFetching: buildFetchingReducer<FetchOpenBookHeaderAction, FetchOpenBookHeaderSucceededAction, FetchOpenBookHeaderFailedAction>(state.openBook.headerFetching, action)
  )  
);

 
I have thought 2 options:

  1. Encapsulate the instantiation of the OpenBook inside an if that tests for the relevant actions, but this leads to code duplication and it goes against the Open/closed principle (SOLID)
  2. Use a switch and for each relevant action create a new OpenBook and initialize only the relevant property, but it feels weird as Dart doesn't have the Spread operator (the three dots in javascript)
  3. Flatten the state

Do you have a recommendation?

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