How can I implement React's lifecycle methods using hooks?
If you're transitioning from class components to functional components, you can replicate the behavior of lifecycle methods using hooks.
The React snippet collection contains function components and reusable hooks for React 18.
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.
When working with stateful code, you can run into issues realted to complexity and readability. Oftentimes, they are easily fixable.
As powerful as React is, it is also quite fragile at places. Did you know that a few lines can easily break your entire React application?
Render a simple toggle component in React.
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.
Testing stateful React components is not difficult, but did you know there's a solution that doesn't involve testing state directly?
Testing Redux-connected components is pretty common. Learn how to use this simple utility function to speed up your testing.
Ever wondered how to detect links in plaintext strings and make them clickable? A little ingenuity combined with React can get you there.
In React components, you might need to conditionally apply a className
. Learn how to handle empty values correctly using this handy tip.
Implement setTimeout()
in a declarative manner, using a custom hook.
Create a countdown timer that prints a message when it reaches zero, using React.
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.
Ever wanted to show the password the user is typing? Here's how you can create a password input field with a reveal button in React.
Run a callback at most once when a condition becomes true, using a custom React hook.
Render links formatted to send an email (mailto:
link) or call a phone number (tel:
link).
Avoid unnecessary re-runs by using primitive dependencies in your React effect.
Testing React components that use portals can be difficult until you understand what you really need to be testing.
Multiple useState
hooks in React can complicate things while debugging. Luckily, there's an easy way to label these values.
Learn the differences between PropTypes.objectOf()
and PropTypes.shape()
and where to use each one with this quick guide.