PersistStoreGate

PersistStoreGate can be used to save application state to external storage. The only one limitation is that this storage must have setItem and getItem methods in API.

If you want to use PersistStoreGate, first you have to wrap your application with <PersistStoreGate eventrix={eventrix}> component which obligatory need one prop - eventrix instance. Optionally, you can also add loader prop to show something during fetching data from external storage.

const App = () => (
    <EventrixProvider eventrix={eventrix}>
        <PersistStoreGate eventrix={eventrix} loader={Loader}>
            <div className='app'>
                {...}
            </div>
        </PersistStoreGate>
    </EventrixProvider>
);

Next step is to use connectPersistStore(eventrix, config) function in file where you combine your whole application store. As you can see, this function needs two arguments. First one is eventrix instance and second one is config which is basically created from: - storage (e.g. localStorage), - storageKey (the key under which application state will be saved in provided storage), - whiteList or blackList

import { Eventrix, connectPersistStore } from 'eventrix';
import exampleReceiver from './receivers/example';

const initialState = {
    exampleCounterOne: 0,
    exampleCounterTwo: 0,
    exampleCounterThree: 0,
};

const eventsReceivers = [exampleReceiver];

const eventrix = new Eventrix(initialState, eventsReceivers);

const config = {
    storage: localStorage,
    storageKey: 'exampleData',
    whiteList: ['exampleCounterOne'], // or blackList: ['exampleCounterTwo', 'exampleCounterThree']
};

connectPersistStore(eventrix, config);

export default eventrix;

Let's say, I have three states in my application (exampleCounterOne, exampleCounterTwo and exampleCounterThree) and I want to save in external storage just one. In this case I can use whiteList which is array of strings. I only need to add name of state to array, like this whiteList: ['exampleCounterOne'] .

Instead of whiteList, you can use blackList which is also array of strings but works different. Let's say, I would like to save all states from my store except one. In this case I can use blackList like that blackList: ['exampleCounterTwo']. It means that exampleCounterOne and exampleCounterTwo will be automatically saved to provided external storage.

Last updated