项目作者: leoafarias

项目描述 :
Simple React Hook to persist/cache the useState locally. Easily load stale state for better ux.
高级语言: TypeScript
项目地址: git://github.com/leoafarias/use-state-persist.git
创建时间: 2020-05-23T21:37:47Z
项目社区:https://github.com/leoafarias/use-state-persist

开源协议:MIT License

下载


Use State Persist

CI Coverage Status Codacy Badge npm version

A simple React Hook to persist useState in local storage.

Works on the Web and React Native.

Easily implement

  • Offline state
  • Stale while revalidate flow
  • Global state
  1. npm install use-state-persist
  2. # or
  3. yarn add use-state-persist

For React Native make sure you do the following on app start up:

  1. import { syncStorage } from 'use-state-persist';
  2. // Initialize async storage
  3. await syncStorage.init();

How to persists useState

Same behavior and API as useState so you can use it by easily replacing the useState hook for the calls which you want to persist offline.

  1. import { useStatePersist as useState } from 'use-state-persist';
  2. const Component = () => {
  3. // Before
  4. //const [counter, setCounter] = useState(0);
  5. const [counter, setCounter] = useState('@counter', 0);
  6. return <CounterDisplay value={counter} ></CounterDisplay>;
  7. };

Stale While Revalidate

  1. import { useStatePersist as useState } from 'use-state-persist';
  2. const Component = () => {
  3. // Loads stale state
  4. const [data, setData] = useState('@data');
  5. const fetchData = async () => {
  6. // Fetches new state
  7. const data = await fetch('/endpoint');
  8. setData(data);
  9. };
  10. useEffect(() => {
  11. fetchData();
  12. }, []);
  13. return <DataDisplay value={data} ></DataDisplay>;
  14. };

Global State

Simple event system allows all the storage writes to be dispatched to all hooks . That means that all useStatePersist() can be used as a global state by sharing the same key useStatePersist('@globalKey')

To avoid that just make sure that the key being passed to the hook is unique useStatePersist('@uniqueKey')

  1. const CounterButton = () => {
  2. const [counter, setCounter] = useState('@counter');
  3. return <Button onClick={() => setCounter(counter => counter++)} />;
  4. };

State will be updated across multiple components

  1. const ShowCounter = () => {
  2. const [counter, setCounter] = useState('@counter', 0);
  3. return <CounterDisplay value={counter} ></CounterDisplay>;
  4. };

Cache Invalidation

There are some cases where you might want to clear all the local storage cache for the hook, pending a certain change in state in the app.

This will clear all the local storage items use by the useStatePersist hook when the key changes.

Use Cases

  • User/App State changes
  • User Log out
  • Environment variable changes
  1. import { invalidateCache } from 'use-state-persist';
  2. invalidateCache('CACHE_KEY');
  3. // You can also send a promise which will compare the result
  4. invalidateCache(async () => 'CACHE_KEY');

React Native

Init prepares the SyncStorage to work synchronously, by getting all values for all keys stored on AsyncStorage. You can use the init method on the web without any side effects to keep code consistency.

  1. import { syncStorage } from 'use-state-persist';
  2. await syncStorage.init();