Advanced React state hooks
Is useState()
too limited for you? Perhaps useReducer()
doesn't quite cut it either? Let's explore some advanced state management hooks.
The React snippet collection contains function components and reusable hooks for React 18.
Is useState()
too limited for you? Perhaps useReducer()
doesn't quite cut it either? Let's explore some advanced state management hooks.
Learn how to use React hooks to handle window events, media queries, server-side rendering and more.
Track the browser's location hash value and search params with this pair of custom hooks.
Learn how to effectively handle the click event outside or inside a component using custom hooks.
Learn how to create custom hooks to listen for events in React.
Use the MutationObserver
API to watch for changes made to the DOM tree.
Implement fetch()
in a declarative manner using React hooks.
Ever wanted to dynamically load an external script in React? Here's a trick to help you out.
Do you need to lock the body scroll when a modal is open? Perhaps this custom hook can help.
Turn requestAnimationFrame()
into a custom hook to animate your React components.
Learn how to persist state in React using hooks and localStorage
or sessionStorage
.
If you're transitioning from class components to functional components, you can replicate the behavior of lifecycle methods using hooks.
Wrapping your mind around React hooks and how they interact with setInterval()
can be difficult. Here's a guide to get you started.
Ever wanted to force update a React component? Here's a custom hook that does just that.
Observe visibility changes for a given element, using the IntersectionObserver
API.
Create a portal, allowing rendering of children outside the parent component, with this custom hook.
Implement setTimeout()
in a declarative manner, using a custom hook.
Ever wanted to change the title of a page in a React app? You can create a custom hook to do just that.
Use this little trick to create an error dispatcher in React.
Run a callback at most once when a condition becomes true, using a custom React hook.
Avoid unnecessary re-runs by using primitive dependencies in your React effect.
Multiple useState
hooks in React can complicate things while debugging. Luckily, there's an easy way to label these values.