btroncone / ngrx-store-localstorage

Simple syncing between @ngrx store and local storage
MIT License
610 stars 119 forks source link

Need Help - Store not stored in localstorage #102

Open msprogramando opened 5 years ago

msprogramando commented 5 years ago

Dear Devs, please help me. Perhaps you see directly what i am doing wrong. I cannot store the state and I can't find it in the localstorage.

I added this to my app.module.ts -> BTW: I had to replace IState with any to not getting an error.

import {StoreModule, ActionReducerMap, ActionReducer, MetaReducer} from '@ngrx/store';
import {localStorageSync} from 'ngrx-store-localstorage';
import {languageReducer} from "./store/reducers/language";

const reducers: ActionReducerMap<any> = {languageReducer};

export function localStorageSyncReducer(reducer: ActionReducer<any>): ActionReducer<any> {
    return localStorageSync({keys: ['language'], rehydrate: true})(reducer);
}

const metaReducers: Array<MetaReducer<any, any>> = [localStorageSyncReducer];

@NgModule({
    declarations: [
        AppComponent,
        LanguageSelectComponent
    ],
    exports: [],
    imports: [
        ...
        StoreModule.forRoot(
            reducers,
            {metaReducers}
        )
    ],
    providers: [],
    bootstrap: [AppComponent]
})
export class AppModule {
}

// Action

import {Injectable} from '@angular/core'
import {Action} from '@ngrx/store'
import {Language} from "../interfaces/language";

export const ADD_LANGUAGE = '[LANGUAGE] Add';

export class AddLanguage implements Action {
    readonly type = ADD_LANGUAGE;
    constructor(public payload: Language) {
    }
}

export type Actions = AddLanguage

// Reducer

import {Action} from '@ngrx/store'
import {Language} from "../interfaces/language";
import * as LanguageActions from './../actions/language'

const initialState: Language = {
    id: 0,
    language: 'No Language Selected',
    tag: 'no-LG',
    country: 'Nolang'
};

export function languageReducer(state: Language = initialState, action: LanguageActions.Actions) {
    switch (action.type) {
        case LanguageActions.ADD_LANGUAGE:
            console.log(action.payload); -> is working
            return action.payload;
        default:
            return state;
    }
}

// In component

    constructor(private store: Store<AppState>) {
        store.pipe( select((state:any) => state.language) )
            .subscribe( (language) => { this.language$ = language; console.log(language) } );
    }

    onSelect(language) {
        this.store.dispatch(
            new LanguageActions.AddLanguage({
                id: language.id,
                tag: language.tag,
                language: language.language,
                country: language.country,
            })
        );

// This is an image of local storage after dispatching the action bildschirmfoto 2018-10-19 um 12 30 09

msprogramando commented 5 years ago

It seems the localStorageSync will never get reaced. bildschirmfoto 2018-10-20 um 07 36 28

pilz97 commented 5 years ago

Hello,

did you already found a solution? I have the same issue :(

amaia00 commented 5 years ago

same issue here.

msprogramando commented 5 years ago

No solution found yet

pilz97 commented 5 years ago

Hi @msprogramando,

I got it to work finally! :)

`const reducers: ActionReducerMap = { settingsState: settingsReducer };

export function localStorageSyncReducer(reducer: ActionReducer): ActionReducer { return localStorageSync({ keys: ['settingsState'], rehydrate: true })(reducer); } const metaReducers: MetaReducer<AppState, Action>[] = [localStorageSyncReducer];`

and in the imports it looks like: StoreModule.forFeature('settings', reducers, { metaReducers }),

will-hu-0 commented 2 years ago

I got the same issue when using nx.

My app structure is like:

// Entry, no real business code inside
apps/main/src/app
  app.module.ts

// Business code models  
libs/myApp/src/lib..
  myApp.module.ts

I got it to work finally by moving ngrx root, etc from apps main model to libs/myApp.