Wagner Moreira
Wagner Moreira

Reputation: 1088

Saving and applying states with NGRX

So I am learning NGRX and I am close to understanding how it works, I am dispatching events and I can see them in store-devtools chrome extension.

What I want to know is how can I save a state and apply it after? My purpose is to save a state from a specific page so that when I'm back to that page I will apply the old state and I will not lose my work there

Here's my repo: https://bitbucket.org/wmoreiradev/ngrxonfire/src/effects-refactor/

Upvotes: 2

Views: 1605

Answers (1)

Ajantha Bandara
Ajantha Bandara

Reputation: 1521

You can do this like bellow

Create actions to save and load current state.

Save the current state before leaving the page. You can do this inside the reducer function or inside a effect like bellow

localStorage.setItem("currentState", JSON.strigify(state));

When you load the page dispatch an action to load the saved state inside a reducer

case Actions.LOAD_SAVED_STATE:
  return {...state, ...action.payload};

also, remove the save state from localStorage.

Upvotes: 1

Related Questions