Storing application data
To store data on the device, there is a special cross-platform solution called AsyncStorage
API. It works the same on both the iOS and Android platforms. You would use this API for applications that don’t require any network connectivity in the first place or to store data that will eventually be synchronized using an API endpoint once a network becomes available.
To install the async-storage
package, run the following command:
npx expo install @react-native-async-storage/async-storage
Let’s look at some code that allows the user to enter a key
and a value
and then stores them:
export default function App() {
const [key, setKey] = useState("");
const [value, setValue] = useState("");
const [source, setSource] = useState<KeyValuePair[]>([]);
The key
, value
, and source
values will handle our state. To save it in AsyncStorage
, we need to define functions:
function setItem() {
return...