1.0.1 • Published 2 years ago
exrun-utils v1.0.1
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): booleanParameters
obj: The object to check.
Return Value
- Returns
trueif the object is empty;falseotherwise.
isEmptyArray
Checks if an array is empty (i.e., has no elements).
Syntax
isEmptyArray(arr: any[]): booleanParameters
arr: The array to check.
Return Value
- Returns
trueif the array is empty;falseotherwise.
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 inlocalStoragefor your value.initialValue(Any): The value you start with iflocalStoragedoesn'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 inlocalStoragewith 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";