Storage management
Bootstrap 5 Storage management plugin
Storage hook allows you to manage data stored in the browser's memory. Thanks to the component, you can easily add, delete, get data and check their expiration time.
Storage management in the latest Bootstrap 5. Manage data stored in the browser memory. Thanks to the component, you can add, delete, get data and check their end time.Note: Read the API tab to find all available options and advanced customization
Basic example
The Storage Hook provides methods to add, remove and get Storage.
Set Storage
Use the function setItem()
to add data to storage. You can test this method using the example
below. The button will call setItem(new Date())
and set a value to name provided in a hook
declaration. To use this example again, press the reset button.
Set the expiration time
You can set the expiration time (in days) of saved data in local storage
Get storage
Your storage value is saved in the first value returned from hook. If there's no such a key in your localStorage - this value will have the initial one passed as second parameter to the hook.
Remove Storage
When the data saved in storage are no longer needed and you want to delete them, use
removeItem
function.
Check Storage
You can set a task to check if the data has expired, delete it and set callback function. Set the interval
(in minutes) on how often to check expires date, and callback fn in callback
property inside the
third parameter.
Advanced example
Show Modal for new users
Show modal after next visit
Storage management - API
Import
Properties
useMDBStorage
Name | Type | Default | Description | Example |
---|---|---|---|---|
value
|
any | '' |
This value is returned from the hook. It contains the current value of item in localStorage or an initial value. |
const [value, { setItem, removeItem }] = useMDBStorage('name', 'initialValue')
|
name
|
String | '' |
A name for the key property in your localStorage. |
const [value, { setItem, removeItem }] = useMDBStorage('name', 'initialValue')
|
initialValue
|
any | '' |
An initial value for your hook if there's no such a one in your localStorage. |
const [value, { setItem, removeItem }] = useMDBStorage('name', 'initialValue')
|
check
|
Object | null | null |
Check the data has not expired. |
const [value] = useMDBStorage('name', 'initialValue', { time: 0.5, callback: () => { console.log('expired') } })
|
Methods
Name | Type | Description | Example |
---|---|---|---|
setItem
|
Function | This method sets a value in localStorage using name provided in a hook. |
<MDBBtn onClick={() => setItem('test')}>Set value to test</MDBBtn>
|
removeItem
|
Function | This method removes an item in the localStorage with name provided in a hook. |
<MDBBtn onClick={removeItem}>Remove an item</MDBBtn>
|