Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

react-native-sticky-state

ncheungg6ISC1.3.3

Custom hook to persist React Native state using AsyncStorage

react, react-native, react native, async storage, asyncstorage, storage, useState, persist state, persist storage, persistant storage, persistant state, useStickyState

readme

react-native-sticky-state

Custom hook to persist React Native state using AsyncStorage

If you're looking to persist React state, check out use-sticky-state

⚙️ Installation

In your react-native project folder, run:

npm i react-native-sticky-state --save

💻 Example

import useStickyState from "react-native-sticky-state";

const App = () => {
  const [count, setCount] = useStickyState(0, -1, "count");

  return (
    <View>
      <Button onPress={() => setCount(count + 1)}>
        <Text>Increment</Text>
      </Button>

      <Text>{count === -1 ? "loading..." : count}</Text>
    </View>
  );
};

📚 Usage

const [state, setState] = useStickyState(defaultValue, loadingValue, storageKey);
Property Description
defaultValue The default value used if storageKey was not previously set
loadingValue The value temporarily used while waiting for useStickyState() to asynchronously grab previous value, can be the same as defaultValue
storageKey Unique string key used to persist data, using AsyncStorage package