useLocalStorageNumber
Description
This hook gets and sets a number
in localStorage.
Features synchronization across hooks sharing the same key name.
Usage
import { useLocalStorageNumber } from 'react-use-window-localstorage';
In your function component:
const [value, setValue] = useLocalStorageNumber('pi', 3.14159);
The hook takes a key name and an optional default value argument. The default value will be immediately written to localStorage and returned if no value is present already. Uses null
when no default value is provided.
Use value
and setValue
to read and write the value.
Because localStorage accepts strings only, the value is encoded using
JSON.stringify()
and decoded using JSON.parse()
.
Additional Features
The hook provides additional features in its return array, should you be interested:
const [value, setValue, loading, available, reset] = useLocalStorageNumber('pi', 3.14159);
- A
loading
value oftrue
indicates that the value is being loaded from localStorage. - An
available
value oftrue
indicates thatlocalStorage
is supported and available for use. - The
reset()
function sets the value back to the provided default, ornull
if none was given.
Signature
useLocalStorageNumber(keyName: string, defaultValue?: number | null) => [number | null, (value: number | null) => void, boolean, () => void, () => void]
Props
Prop | Type | Description |
---|---|---|
keyName | string | Required. Key name to use in localStorage. |
defaultValue | number | null | Optional. Provide a default number value when the key's value is not found in localStorage. Will be immediately written to localStorage if not present. Use null for no default. |
Return
The hook returns an array containing:
Index | Name | Type | Description |
---|---|---|---|
0 | value | number | null | The number value from localStorage, when loading is false . Will be the default value (or null if none provided) while loading is true . If null when not loading, the key was not found in localStorage. |
1 | setValue | (value: number | null) => void | Sets the value in localStorage. Provide null to set it back to the default value. Providing null will delete the key from localStorage if the default value is null . |
2 | loading | boolean | true while the value loads from localStorage, false otherwise. |
3 | available | boolean | true if localStorage is supported and available for use, false otherwise. |
4 | reset | () => void | Reset the value to the default. |
Example
import * as React from 'react';import { useLocalStorageNumber } from 'react-use-window-localstorage';export function UseLocalStorageNumberExample() {const defaultValue = 3.14159;const [enteredValue, setEnteredValue] = React.useState('');const [value, setValue, loading, available, reset] = useLocalStorageNumber('numValue', defaultValue);const [value2, setValue2, loading2] = useLocalStorageNumber('numValue', defaultValue);return (<div>{!loading && (<div><div>Key "numValue":{' '}<inputtype="text"value={enteredValue}onChange={e => setEnteredValue(e.target.value)}onKeyPress={e => e.key === 'Enter' && setValue(parseFloat(enteredValue))}/><button onClick={() => setValue(parseFloat(enteredValue))}>Set</button></div><div><strong>Stored value:</strong> {value}</div></div>)}<div><button onClick={() => reset()}>Reset "numValue" to default</button></div><hr />{!loading2 && <div>Another "numValue" hook (different hook, will synchronize): {value2}</div>}</div>);}