PatrickJS-starter icon indicating copy to clipboard operation
PatrickJS-starter copied to clipboard

Question about HMR - flat state

Open mildful opened this issue 9 years ago • 0 comments

  • I'm submitting a ... [ ] bug report [ ] feature request [x] question about the decisions made in the repository

First of all, thanks for this seed.

I came from another starter using ngrx/store but for what I'm going to build, I think ngrx/store is not necessary (great article).

But HMR is a very interesting feature while in development mode. The thing is that your state (AppState) must be flat since we cannot mutate the state directly and the only way to update it is via

public set(prop: string, value: any) {
    // internally mutate our state
    return this._state[prop] = value;
  }

Of course we can use it like this :

export interface DeepStateExample {
    value: string;
    subState: {
        mySubInt: number;
        mySubStr: string;
    };
};

export class SomeSmartComponent {

    private internalState: any = { value: '', subState: { mySubInt: 0, mySubStr: '2' } };

    submitState(value: string): void {
        this.initialState.value = value;
        this.appState.setValue('subState', this.internalState);
    }
}

// TODO in app.service.ts :
export type InternalStateType = {
  subState: DeepStateExample,
  [key: string]: any
};

I would have to do this for every 'substate' in my application, but I am wondering if it's a good way to go. Also using public set(prop: string, value: any) can be annoying since autocompletion is not available. This works only with one substate level. If I want to manage substates deeply, it's going to be harder.

Since everything stored in the state is also stored elsewhere (in the components as internalstate) , it seems that everything concerning the state should only be used in dev mode. Is that right ?

I was just wondering : did someone else have the same question and what do you think about it ?

mildful avatar Jan 24 '17 13:01 mildful