Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update readme with custom driver example #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 55 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,60 @@ const store = configureStore({
// Continue using the redux store as usual...
```

Usage - react-native with multiple storage types
------------------------------------------------

**Custom Driver that uses both expo-secure-store and AsyncStorage**

```ts
import { configureStore } from '@reduxjs/toolkit';
import { Driver, rememberReducer, rememberEnhancer } from 'redux-remember';
import * as SecureStore from 'expo-secure-store';
import AsyncStorage from '@react-native-async-storage/async-storage';
import reducers from './reducers';

const prefix = '@@remember-';

const secureKeys = ['secureKey1', 'secureKey2'];

const rememberedKeys = [
'insecureKey1', 'insecureKey2',
...secureKeys
];

export const customDriver: Driver = {
setItem(key: string, value: any) {
const originalKey = key.slice(prefix.length);
if (secureKeys.includes(originalKey)) {
return SecureStore.setItemAsync(originalKey, value);
}

return AsyncStorage.setItem(key, value);
},
getItem(key: string) {
const originalKey = key.slice(prefix.length);
if (secureKeys.includes(originalKey)) {
return SecureStore.getItemAsync(originalKey);
}

return AsyncStorage.getItem(key);
}
};

const store = configureStore({
reducer: rememberReducer(reducers),
enhancers: (getDefaultEnhancers) => getDefaultEnhancers().concat(
rememberEnhancer(
customDriver,
rememberedKeys,
{ prefix }
)
)
});

// Continue using the redux store as usual...
```

Usage - inside a reducer
------------------------

Expand Down Expand Up @@ -202,7 +256,7 @@ export default store;
Usage - React rehydration gate
------------------------------

**Preqrequisite: to be used with: [Usage - inside a reducer](#usage---inside-a-reducer) or similar**
**Prerequisite: to be used with: [Usage - inside a reducer](#usage---inside-a-reducer) or similar**

```tsx
import { FC, PropsWithChildren } from 'react';
Expand Down