Skip to content

maxkomarychev/redux-persist-filesystem-storage

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 

Repository files navigation

Redux persist filesystem storage

npm version npm downloads

Storage adaptor to use react-native-fetch-blob with redux-persist, by implementing the needed methods: setItem, getItem, removeItem, getAllKeys and clear.

This storage can be used on Android to prevent issues with the storage limitations in the RN AsyncStorage implementation. (See redux-persist#199, redux-persist#284)

install

yarn add redux-persist-filesystem-storage

Then, as react-native-fetch-blob is a dependency of this project, we need to ensure its linked with

react-native link react-native-fetch-blob

(or check their docs).

usage

import FilesystemStorage from 'redux-persist-filesystem-storage'
import { persistStore, autoRehydrate } from 'redux-persist'

const store = createStore(reducer, undefined, autoRehydrate())

persistStore(
  store,
  {
    storage: FilesystemStorage,
  },
)

usage with custom options

import RNFetchBlob from 'react-native-fetch-blob'
import FilesystemStorage from 'redux-persist-filesystem-storage'
import { persistStore, autoRehydrate } from 'redux-persist'

const store = createStore(reducer, undefined, autoRehydrate())

persistStore(
  store,
  {
    storage: FilesystemStorage.config(
      {
        storagePath: `${RNFetchBlob.fs.dirs.DocumentDir}/persistStore`,
      },
    },
  )
)

migration from previous storage

tested with redux-persist v4

the snippet below lets you migrate redux data previously stored in AsyncStorage to redux-persist-filesystem-storage.

NOTE This snippet lets you migrate healthy data. It will not restore data if it is already hit limits of AsyncStorage

import { persistStore, getStoredState } from 'redux-persist'
import FilesystemStorage from 'redux-persist-filesystem-storage'
import { AsyncStorage } from 'react-native'
import _ from 'lodash'
import { createStore } from 'redux'

const store = createStore(...)

// create persistor for `redux-persist-filesystem-storage`
const fsPersistor = persistStore(
  store,
  { storage: FilesystemStorage },
  async (fsError, fsResult) => {
    if (_.isEmpty(fsResult)) {
      // if state from fs storage is empty try to read state from previous storage
      try {
        const asyncState = await getStoredState({ storage: AsyncStorage })
        if (!_.isEmpty(asyncState)) {
          // if data exists in `AsyncStorage` - rehydrate fs persistor with it
          fsPersistor.rehydrate(asyncState, { serial: false })
        }
      } catch (getStateError) {
        console.warn("getStoredState error", getStateError)
      }
    }
  }
)

About

Redux persist adaptor for React Native filesystem storage

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%