useLocalStorage()

useLocalStorage( STRING key, MIXED initialValue )

The name of the value variable (the first in the array) and the name of the localStorage key should match. See number and "number" below.

Setting the value to null will remove the localStorage item.

This is a modified version of code from useHooks, so check that out for extra details.

Parameters

  • key (STRING, Required, default: none) They key of the localStorage item. This shoul
  • initialValue (MIXED, Required, default: none)

Returns

(ARRAY) [value, setValue]

These work exactly like React's useState().

  • value (MIXED)
  • setValue (CALLABLE)

Examples

import { useLocalStorage } from "@preemptivelove/utilities-js"
const Component = ()=> {
const [number, setNumber] = useLocalStorage( "number", 7 )
return (
<input value={ number } />
<button onClick={ setNumber }>Change number</button>
)
}

This doesn't actually work.

Source

https://github.com/PreemptiveLove/utilities-js/blob/master/src/hooks/useLocalStorage.js