Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
@devboldly/react-use-local-storage
Advanced tools
React hooks for accessing the localStorage Web Storage API.
Read the official documentation.
👁️ Live Demo
A set of hooks to easily store and retrieve data from localStorage.
Encoding is handled for common data types, including booleans, numbers, strings, and objects, or you can encode data yourself if you'd like.
Changes to localStorage are synchronized across all hooks automatically.
What is localStorage? The
localStorage
property allows you to store{key: value}
string data that is saved across browser sessions. Data stored inlocalStorage
has no expiration time.
localStorage
support
npm i @devboldly/react-use-local-storage
Use the useLocalStorageString hook:
import { useLocalStorageString } from '@devboldly/react-use-local-storage';
In your function component:
const defaultValue = 'cyan';
const [value, setValue] = useLocalStorageString('favColor', defaultValue);
Use the useLocalStorageObject hook:
import { useLocalStorageObject } from '@devboldly/react-use-local-storage';
In your function component:
const defaultValue = { a: 'hello', b: 123 };
const [value, setValue] = useLocalStorageObject('myObj', defaultValue);
Note that your objects must be compatible with JSON.stringify(). Use useLocalStorageItem otherwise.
Use the useLocalStorageBoolean hook:
import { useLocalStorageBoolean } from '@devboldly/react-use-local-storage';
In your function component:
const defaultValue = true;
const [value, setValue] = useLocalStorageBoolean('swordEquipped', defaultValue);
Use the useLocalStorageNumber hook:
import { useLocalStorageNumber } from '@devboldly/react-use-local-storage';
In your function component:
const defaultValue = 3.14159;
const [value, setValue] = useLocalStorageNumber('importantNumber', defaultValue);
If you'd like to store something other than the data types above, define your own encoding using the useLocalStorageItem hook.
Here's a starting point:
import { useLocalStorageItem } from '@devboldly/react-use-local-storage';
In your function component:
const defaultValue = 'something custom';
const encode = value => JSON.stringify(value);
const decode = itemString => JSON.parse(itemString);
const [value, setValue] = useLocalStorageItem('name', defaultValue, encode, decode);
All hooks provide additional features in their return arrays, should you be interested:
const [value, setValue, loading, reset, restore] = useLocalStorageString('favColor', 'cyan');
loading
value of true
indicates that the value is being loaded from localStorage.reset()
function sets the value back to the provided default, or null
if none was given.restore()
function retrieves the latest value from localStorage. Use this if the localStorage value changes outside of this hook and you need to restore it to the latest.Type definitions have been included for TypeScript support.
Logo graphics by Twemoji, licensed under CC-BY 4.0. Favicon by favicon.io.
Feel free to submit a pull request for bugs or additions, and make sure to update tests as appropriate. If you find a mistake in the docs, send a PR! Even the smallest changes help.
For major changes, open an issue first to discuss what you'd like to change.
See the library template for npm script documentation.
If you found this project helpful, let the community know by giving it a star: 👉⭐
Copyright © 2020 DevBoldly https://devboldly.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
FAQs
React hooks for accessing the localStorage Web Storage API.
We found that @devboldly/react-use-local-storage demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.