1.1.0 • Published 2 years ago

react-native-hook-use-persist v1.1.0

Weekly downloads
15
License
MIT
Repository
github
Last release
2 years ago

react-native-hook-use-persist

react-native-hook-use-persist is a hook you can use to add a persistent state. The usage is like useState but the value is kept in AsyncStorage.

Content

Installation

npm install react-native-hook-use-persist

Usage

Initialize the persistent values in your App.js

import { StatusBar } from 'expo-status-bar';
import React, { useEffect, useState } from 'react';
import { StyleSheet, View } from 'react-native';
import { initPersist } from 'react-native-hook-use-persist'

import Test from './Test'

export default function App () {
    const [isLoadingComplete, setIsLoadingComplete] = useState(false)

    useEffect(() => {
        const init = async () => {
            try {
                # Asynchronous load from database
                await initPersist()
            }
            catch (err) {
                console.error(err)
            }
            finally {
                setIsLoadingComplete(true)
            }
        }
        init()
    }, [])

    if (!isLoadingComplete) {
        return null;
    }

    return (
        <View style={styles.container}>
            <StatusBar style="auto" />
            <Test />
        </View>
    );
}

Use the hook inside other views

import React from "react"
import { View, Text, Button } from "react-native"
import { usePersist } from "./src/index"

export default function Test() {
  const [getBla, setBla] = usePersist("key", 0)

  return (
    <View>
      <Text>{getBla()}</Text>
      <Button
        onPress={() => {
          setBla((bla) => bla + 1)
        }}
        title="Pressme"
      />
    </View>
  )
}
1.1.0

2 years ago

1.0.11

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

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