1.1.4 • Published 4 years ago

react-use-persist v1.1.4

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

React-Use-Persist

Persistent State Hooks. Combines the syntax and APIs of useState and useReducer with localStorage and sessionStorage to make state persistence easy and clean.

API Reference

  • useLocalState | useSessionState

    // localStorage
    const [state, setState, remove] = useLocalState(key, initialState, { writeInit }); 
    // sessionStorage
    const [state, setState, remove] = useSessionState(key, initialState, { writeInit });
    Options:
    • key: string
      • Required
      • Storage unique identifier
    • initialState: any
      • Optional initial value
    • writeInit
      • By default, state does not persist on initialization. However, if for some reason this is needed, simply mark this true.
  • useLocalReducer | useSessonReducer

    // localStorage
    const [state, dispatch, remove] = useLocalReducer(key, reducer, initialState, { writeInit });
    // sessionStorage
    const [state, dispatch, remove] = useSessionReducer(key, reducer, initialState, { writeInit });
    Options:
    • key: string
      • Required
      • Storage unique identifier
    • reducer
      • Required
      • Any reducer function
    • initialState: any
      • Optional initial value
    • writeInit
      • By default, state does not persist on initialization. However, if for some reason this is needed, simply mark this true.
Returns:
  • state, setStateOrDispatch, remove
    • The first two arguments will be identical to those returned by React's useState and useReducer hooks.
    • A third argument remove is provided. Calling remove will clear the state and the key from the persistent storage.

Example

https://codesandbox.io/s/react-use-persist-07elu?file=/src/App.js

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago