exrun-utils
v1.0.1
Published
This library provides utility functions and React hooks for common tasks in JavaScript and React.
Downloads
0
Readme
My NPM Library
This library provides utility functions and React hooks for common tasks in JavaScript and React.
Utility Functions
isEmptyObj
Checks if an object is empty (i.e., has no own properties).
Syntax
isEmptyObj(obj: object): boolean
Parameters
obj
: The object to check.
Return Value
- Returns
true
if the object is empty;false
otherwise.
isEmptyArray
Checks if an array is empty (i.e., has no elements).
Syntax
isEmptyArray(arr: any[]): boolean
Parameters
arr
: The array to check.
Return Value
- Returns
true
if the array is empty;false
otherwise.
useLocalStorage
Description
useLocalStorage
is a custom React Hook that simplifies the interaction with localStorage
. It enables you to store and retrieve values from localStorage
in a more React-like way, using state.
Syntax
const [storedValue, setStoredValue] = useLocalStorage(key, initialValue);
Parameters
key
(String): The key inlocalStorage
for your value.initialValue
(Any): The value you start with iflocalStorage
doesn't have anything stored at the provided key.
Return Value
This hook returns a pair of values: storedValue
and setStoredValue
.
storedValue
(Any): The current value of the item inlocalStorage
with the given key.setStoredValue
(Function): A function that can be used to update the value ofstoredValue
.
Usage
import useLocalStorage from "./useLocalStorage";
function Component() {
const [name, setName] = useLocalStorage("name", "Bob");
// The rest of your component
}
To re-export this utility, you can use the following line of code:
export { default as useLocalStorage } from "./useLocalStorage";