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.
One hook that it comes with is the useArray
hook.
To use it, we can write:
import React from "react";
import { useArray } from "react-recipes";
export default function App() {
const { add, clear, removeIndex, removeById, value: currentArray } = useArray(
["cat", "dog", "bird"]
);
return (
<>
<button onClick={() => add("chicken")}>Add animal</button>
<button onClick={() => removeIndex(currentArray.length - 1)}>
Remove animal
</button>
<button onClick={clear}>Clear</button>
<p>{currentArray.join(", ")}</p>
</>
);
}
The useArray
hook takes an array as the initial value of the array.
It returns an object with various properties.
add
is a function that lets us add an item.
clear
is a function to clear the array.
removeIndex
is a function to remove an entry by its index.
removeById
lets us remove an item by its id
property.
The useAsync
hook lets us run async code in a cleaner way.
We cab separate our async code into its own function.
For example, we can write:
import React from "react";
import { useAsync } from "react-recipes";
const draw = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
const rnd = Math.random() * 10;
rnd <= 5 ? resolve(rnd) : reject("error");
}, 2000);
});
};
export default function App() {
const { error, execute, pending, value } = useAsync(draw, false);
return (
<div>
<button onClick={execute} disabled={pending}>
{pending ? "Loading..." : "Click Me"}
</button>
{value && <div>{value}</div>}
{error && <div>{error}</div>}
</div>
);
}
to use the hook.
We have the useAsync
hook that takes the draw
function.
draw
returns a promise that resolves to a random value or rejects the promise depending on the value of rnd
.
The 2nd argument of the hook is whether we let the hook run immediately.
The hook returns an object with various properties.
error
is a string that comes from the rejected promise.
execute
is a function to delay the execution of our function.
pending
is a boolean to indicate when it’s executing.
value
is the value returned from the execution of our function.
The useCookie
hook lets us manipulate client-side cookies.
To use it, we can write:
import React from "react";
import { useCookie } from "react-recipes";
export default function App() {
const [userToken, setUserToken, deleteUserToken] = useCookie("token", "0");
return (
<div>
<p>{userToken}</p>
<button onClick={() => setUserToken("100")}>Change token</button>
<button onClick={() => deleteUserToken()}>Delete token</button>
</div>
);
}
to use the useCookie
hook.
The arguments for the hook are the key and initial value of the cookie.
It returns an array with some variables we can use.
userToken
is the token value.
setUserToken
is the function to set the cookie with the key token
.
deleteUserToken
deletes the cookie with the token
key.
Conclusion
React Recipes comes with hooks for manipulation arrays and cookies.