Hooks contains our logic code in our React app.
We can create our own hooks and use hooks provided by other people.
In this article, we’ll look at some useful React hooks.
React Recipes
React Recipes comes with many hooks that we can use to do various things.
We can use the useCopyClipboard
hook to let us copy any string to the clipboard.
For instance, we can write:
import React from "react";
import { useCopyClipboard } from "react-recipes";
export default function App() {
const [isCopied, setIsCopied] = useCopyClipboard();
const copy = () => {
setIsCopied("copy string");
};
return (
<button onClick={copy} type="button">
{isCopied ? "Copied" : "Copy"}
</button>
);
}
to create a button to copy a string to the clipboard.
The useCopyClipboard
hook returns an array with the isCopied
and setIsCopied
variables.
isCopied
indicates that the string is copied to the clipboard if it’s true
.
setIsCopied
copies the string to the clipboard.
The useDarkMode
hook lets us toggle on and off dark mode.
The setting will be saved in local storage.
For instance, we can write:
import React from "react";
import { useDarkMode } from "react-recipes";
export default function App() {
const [darkMode, setDarkMode] = useDarkMode();
return (
<div>
<button onClick={() => setDarkMode(!darkMode)}>toggle dark mode</button>
<p>{darkMode.toString()}</p>
</div>
);
}
The setDarkMode
function sets whether dark mode is on or not.
darkMode
has the toggle state of dark mode.
The setting would be saved as an entry with the key dark-mode-enabled
in local storage.
It doesn’t come with any styles, so we’ve to set the dark mode styles ourselves.
useDebounce
is a hook that lets us denounce any fast-changing value.
For instance, we can use it by writing:
import React from "react";
import { useDebounce } from "react-recipes";
export default function App() {
const [searchTerm, setSearchTerm] = React.useState("");
const [result, setResult] = React.useState({});
const debouncedSearchTerm = useDebounce(searchTerm, 500);
const search = async () => {
const res = await fetch(`https://api.agify.io/?name=${debouncedSearchTerm}`);
const data = await res.json();
setResult(data);
};
React.useEffect(() => {
if (debouncedSearchTerm) {
search();
}
}, [debouncedSearchTerm]);
return (
<div>
<input value={searchTerm} onChange={e => setSearchTerm(e.target.value)} />
<p>{JSON.stringify(result)}</p>
</div>
);
}
We use the useDebounce
hook with the searchTerm
state.
The 2nd argument is the delay to set the debouncedSearchTerm
value in milliseconds.
Then we pass that into the array in the 2nd argument of useEffect
to watch its value.
It’ll run the search
function to get the data if debouncedSearchTerm
is set.
The useDimensions
hook let us get the dimension of any element.
To use it, we can write:
import React from "react";
import { useDimensions } from "react-recipes";
export default function App() {
const [wrapperRef, dimensions] = useDimensions();
return (
<div ref={wrapperRef}>
height: {dimensions.height}
width: {dimensions.width}
</div>
);
}
The hook returns the wrapperRef
that we pass as the value of the ref
prop of the element that we want to watch the size of.
dimensions
has the dimensions of the div that we passed the ref to.
Then as we change the size of the viewport, we’ll see the dimensions update.
Conclusion
React Recipes has hooks for rebounding, copying to clipboard, watching element sizes, and manipulating cookies.