@talesoft/use-storage
TypeScript icon, indicating that this package has built-in type declarations

0.1.2 • Public • Published

@talesoft/use-storage

A wrapper including fallbacks over localStorage and sessionStorage.

Consumed as a react hook.

Other storage kinds might be supported in the future.

Usage

usePersistentState<Value>(key: string, defaultValue: Value): [Value, Dispatch<SetStateAction<Value>>]

A wrapper over localStorage. Falls back to a pure memory storage.

import { usePersistentState } from '@talesoft/use-storage'

export function MyComponent() {
  const [token, setToken] = usePersistentState('authToken', undefined)

  const onSubmit = async () => {

    setToken(await login())
  }

  return (
    <.../>
  )
}

useTemporaryState<Value>(key: string, defaultValue: Value): [Value, Dispatch<SetStateAction<Value>>]

A wrapper over sessionStorage. Falls back to a pure memory storage.

import { useTemporaryState } from '@talesoft/use-storage'

export function MyComponent() {
  const [soundDisabled, setSoundDisabled] = useTemporaryState('soundDisabled', false)

  const onDisableSound = () => {

    setSoundDisabled(true)
  }

  return (
    <.../>
  )
}

useMemoryState<Value>(key: string, defaultValue: Value): [Value, Dispatch<SetStateAction<Value>>]

A simple in-memory storage. Poor-mans contexts, basically.

import { useMemoryState } from '@talesoft/use-storage'

export function MyComponent() {
  const [currentUser, setCurrentUser] = useMemoryState('currentUser', undefined)

  const onLogin = () => {

    setCurrentUser(await fetchUser())
  }

  return (
    <.../>
  )
}

Readme

Keywords

none

Package Sidebar

Install

npm i @talesoft/use-storage

Weekly Downloads

1

Version

0.1.2

License

MIT

Unpacked Size

11.1 kB

Total Files

15

Last publish

Collaborators

  • torben-koehn
  • torbenkoehn
  • simonekruegel