react-native-use-persist-storage icon indicating copy to clipboard operation
react-native-use-persist-storage copied to clipboard

useReducer like functionality

Open dsernst opened this issue 3 years ago • 1 comments

This is awesome. Is there any way or plans to also add persistent useReducer like functionality?

dsernst avatar Mar 07 '22 21:03 dsernst

In case anyone else is looking for this, I did something like this myself, not quite as performant, but gets the job done, with this helper function:

// useStorage.ts

import { createPersistContext } from 'react-native-use-persist-storage'

export const useStorage = () => {
  const [state, setState, loaded] = usePersistStorage('storageKey', initialState)

  const update = (diff: Record<string, any>) => {
    setState((prev) => ({ ...prev, ...diff }))
  }

  return { state, update, loaded }
}

dsernst avatar Mar 07 '22 22:03 dsernst