- Hooks
- Browser hooks
- useLocalStorage
useLocalStorage
Store, retrieve, and synchronize data from the browser’s localStorage API with useLocalStorage
use-local-storage.tsx
Installation
Copy and paste the following code into your project.
use-local-storage.ts
Update the import paths to match your project setup.
Parameters
Name | Type | Description |
---|---|---|
key | string | The key used to access the local storage value. |
initialValue | any | The initial value to use if there is no item in the local storage with the provided key. |
Return values
Name | Type | Description |
---|---|---|
localState | any | The current state of the value stored in local storage. |
handleSetState | function | A function to set the state of the value in the local storage. This function accepts a new value or a function that returns a new value. The value is also saved in the local storage under the provided key. |