Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to achieve state persistence even after a reload in Ngrx

I have a shopping cart which I am maintaining with Ngrx and for maintaining the persistence of the state after a page refresh I have two options:

  1. Stringify the state and store it in local storage and get it back after the reload.
  2. Stringify the state and store it inside a text file and then get the string out of the file and use JSON.parse to convert the string back to a JSON object.

I am storing some price related data so didn't want to store in local storage.

What should I use? Or is there any better way to do this?

like image 395
Anil Kumar Avatar asked Oct 17 '25 05:10

Anil Kumar


1 Answers

Using the following library you can achieve your desired result

https://www.npmjs.com/package/ngrx-store-localstorage

I am using it myself in multiple projects and it works great, it acts as a meta reducer and therefore 'automatically' takes care of things for you.

https://ngrx.io/guide/store/metareducers

like image 188
Ali Nasserzadeh Avatar answered Oct 19 '25 22:10

Ali Nasserzadeh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!