James Broad
1 min readJun 21, 2018

--

Hey Francesco,

Redux on React Native assumes there’s a singleton store instance.

We need to support more than one user using our app (log in, then logout and log in as another user).

In order to achieve a persisted (using Redux Persist) store, you have two options how you could make this theoretically work.

Let’s assume you’ve got this Redux schema (set of reducers):

{ reports: [{checked_in:2018-06-01, checked_out:2018-06-01}] }

To the best of my knowledge, you have two routes to support persisted Redux store based on the user:

  1. Have a Redux store that’s an object hierarchy of users and duplicate reducer schemas that hang off each user. This would look something like:
    { user123: { reports: [{checked_in:2018-06-01, checked_out:2018-06-01}] }}
    The issue with this is that Redux doesn’t want to work with arbitrary objects at the high level so you’d have to invent some fancy code to bend it to your will.
  2. Switch out the Redux store when a user is changed, storing the state against the key of the user.

We chose the latter which involved using a custom Redux middleware that listened for a login action that would hydrate the Redux store with whatever was stored in LocalStorage using the Redux Persist API. Similarly, if the user logged out, we’d ensure we flush to LocalStorage and reset the Redux store.

Hope that helps?

--

--

James Broad
James Broad

Written by James Broad

Exploring strategies and tactics to create a successful tech business

No responses yet