angularspring-mvcstate-managementapplication-state

Angular 4 application state management


I will try to phrase this question more as a problem than a discussion point.

Im building an Angular 4 + Spring application and I am having problems with state management. App functionalities are login, interactive map, subpages with settings etc.

My problem: For example, user logs in, does something with interactive map(changes map layers which are shown or zooms in on map) and then goes to an subpage or logs off completely from app. The desired functionality would be that next time the user logs in, the same things that he/she did last time will be there also this time.

I have done some research and currently I see 3 options:

  1. Front-end approach with cookies
  2. Back-end approach with API's that hold these values
  3. Redux

At this point I'm not sure which approach I should take..


Solution

  • If you want to store the data for the user permanently, the easiest way is to put them into the localStorage of the browser. This way you can retrieve the users data when your components is loading. To do this you simply do this:

    //Put data to local storage
    localStorage.setItem('userData', JSON.stringify(data));
    
    //Get data from local storage
    let userData = JSON.parse(localStorage.getItem('userData'))
    

    The problem with a redux approch is, if you reload your application your savings will be gone.

    Also an approach that involves the backend is not the best idea. Because you have extra calls to the backend, need new tables to hold the data for each user etc.