Git Product home page Git Product logo

Comments (4)

nandorojo avatar nandorojo commented on May 30, 2024 1

Ah I didn't realize that setGlobalState was globally accessible, I was looking for that. Thanks!

from react-hooks-global-state.

nandorojo avatar nandorojo commented on May 30, 2024 1

In case anyone sees this, I came up with a wrapper, useInitialStateFromAsyncStorage, which I use anywhere to store / get state from local storage.

const initialState = {
  query: ''
}
const { useGlobalState, setGlobalState } = createGlobalState(initialState)

const useQueryState = () => {
  const [query, setQuery] = useGlobalState('query')
 
  return useInitialStateFromAsyncStorage([query, setQuery], { asyncStorageKey: 'query' })
}

I then use it like this:

const [query, setQuery] = useQueryState()

The code for useInitialStateFromAsyncStorage is as follows:

import type { SetStateAction } from 'react'
import { useRef, useEffect } from 'react'
import AsyncStorage from '@react-native-community/async-storage'
import { useDebounce } from '@beatgig/hooks/use-debouce'

type Props<State> = [state: State, setState: (u: SetStateAction<State>) => void]

type Options = {
  asyncStorageKey: string
  debounce?: number
}

export default function useInitialStateFromAsyncStorage<State>(
  [state, setState]: Props<State>,
  { asyncStorageKey, debounce = 200 }: Options
) {
  const hasStateBeenUpdated = useRef(false)
  const mounted = useRef(false)
  useEffect(() => {
    if (mounted.current) {
      hasStateBeenUpdated.current = true
    } else {
      mounted.current = true
    }
  }, [state])

  useEffect(() => {
    const getFromAsyncStorage = async () => {
      const fromStorage: null | string = await AsyncStorage.getItem(
        asyncStorageKey
      )
      console.log('[use-initial-state-from-async-storage] get item', {
        asyncStorageKey,
        fromStorage,
      })
      // only set it from local storage if we haven't changed the state yet, meaning it's equal to its initial state
      if (fromStorage && !hasStateBeenUpdated.current) {
        let newState: { value: State } | null = null
        try {
          newState = JSON.parse(fromStorage)
          if (newState?.value) {
            hasStateBeenUpdated.current = true // not necessary but whatever
            setState(newState?.value)
          }
        } catch (e) {
          console.error(
            `[use-initial-state-from-async-storage] error getting from local storage ${e}`,
            { newState }
          )
        }
      }
    }
    getFromAsyncStorage()
    // these will never change
  }, [asyncStorageKey, setState])

  const debouncedState = useDebounce(state, debounce)

  const debouncedStateString = JSON.stringify({ value: debouncedState })

  useEffect(() => {
    if (debouncedStateString) {
      console.log('[use-initial-state-from-async-storage] set item', {
        asyncStorageKey,
        debouncedStateString,
      })
      AsyncStorage.setItem(asyncStorageKey, debouncedStateString)
    }
  }, [asyncStorageKey, debouncedStateString])

  return [state, setState] as const
}

I assume you could achieve the same with localStorage in place of AsyncStorage.

from react-hooks-global-state.

dai-shi avatar dai-shi commented on May 30, 2024

Hi, thanks for coming.

The first one won't work. The second one looks good.

Another option is to do it outside React. For example:

const initialState = {
  query: ''
}

const { useGlobalState, setGlobalState } = createGlobalState(initialState)

(async () => {
  const cachedQuery = await AsyncStorate.get('query')
  if (cachedQuery) {
    setGlobalState('query', cachedQuery)
  }
})()

Hope it helps!

from react-hooks-global-state.

dai-shi avatar dai-shi commented on May 30, 2024

Actually, we have wiki https://github.com/dai-shi/react-hooks-global-state/wiki
Feel free to add new pages.

from react-hooks-global-state.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.