localStorage.setValue() to root level?


I'm building a mobile app where each user can fill up their own shopping cart with products before checking out. Data persistency between sessions for each user is crucial here, that's why I've opted for localStorage rather than a temp state.

As of today, I've been using localStorage to save an array of objects named cart (path is localStorage.values.cart):


I'd like to add to localStorage.values a second array of objects named payments (path would be localStorage.values.payments).

According to Retool documentation, a key is required when using localStorage.setValue() function.
localStorage.values is the object I'd like to update with a variable I've set in my JS script containing both 'cart' and 'payments' arrays of objects.

Is this possible somehow - a command similar to the backslash in DOS for example - or do I need to revise my localStorage data structure?


Yea I don't think localStorage accepts an object, it always has to be a string key as the identifier.

Rather than restructure your data you could just set it twice maybe?


localStorage.setValue('payments', myobject.payments);
localStorage.setValue('cart', myobject.cart);

localStorage.setValue()writes the whole localStorage.values.

localStorage.setValue('cart', myobject.cart); will wipe your payments array written by localStorage.setValue('payments', myobject.payments);

This would have worked with a .setIn() function but it's not available in localStorage, contrary to a temp state.

I have already changed my localStorage data structure by adding a parent object to the arrays of objects. This is very helpful as I can:

  1. Load the existing arrays of items
  2. Push new items to each arrays
  3. Declare the new arrays of objects in an object called data
  4. Update localStorage entirely with localStorage.setValue("data": data)

The result:

Too bad there's no way (apparently) to write to localStorage.values an object containing arrays of objects (or even better, the possibility to call .setIn() function in localStorage).

That's not my experience, this seems to work fine for me: setValue creates or replaces the key not the entire localStorage object:

Well, that's great news! I have certainly messed things up on my end because I was constantly overwriting my localStorage using successive localStorage.setValue().

Thanks for your help!