Snippets
React snippets
Components and hooks you can paste into a React project. Each one shows the implementation, a short explanation of how it works and an example use.
- Automatic text linking Renders a string as plaintext, with URLs converted to appropriate link elements. Use String.prototype.split() …
- Button with ripple effect Renders a button that animates a ripple effect when clicked. Use the useState() hook to create the coords and …
- Callable telephone link Renders a link formatted to call a phone number (tel: link). Use phone to create a <a> element with an …
- Carousel Renders a carousel component. Use the useState() hook to create the active state variable and give it a value …
- Closable alert Renders an alert component with type prop. Use the useState() hook to create the isShown and isLeaving state …
- Collapsible accordion Renders an accordion menu with multiple collapsible content elements. Define an AccordionItem component, that …
- Collapsible content Renders a component with collapsible content. Use the useState() hook to create the isCollapsed state …
- Controlled input field Renders a controlled <input> element that uses a callback function to inform its parent about value …
- Countdown timer Renders a countdown timer that prints a message when it reaches zero. Use the useState() hook to create a …
- Data list Renders a list of elements from an array of primitives. Use the value of the isOrdered prop to conditionally …
- Data table Renders a table with rows dynamically created from an array of primitives. Render a <table> element with …
- Email link Renders a link formatted to send an email (mailto: link). Use the email, subject and body props to create a …
- Expandable object tree view Renders a tree view of a JSON object or array with collapsible content. Use the value of the toggled prop to …
- File drag and drop area Renders a file drag and drop component for a single file. Create a ref, called dropRef and bind it to the …
- Modal dialog Renders a Modal component, controllable through events. Define a keydownHandler that handles all keyboard …
- Object table view Renders a table with rows dynamically created from an array of objects and a list of property names. Use …
- React useAsync hook Handles asynchronous calls. Create a custom hook that takes a handler function, fn. Define a reducer function …
- React useBodyScrollLock hook Enables body scroll locking. Use the useLayoutEffect() with an empty array as the second argument to execute …
- React useClickInside hook Handles the event of clicking inside the wrapped component. Create a custom hook that takes a ref and a …
- React useClickOutside hook Handles the event of clicking outside of the wrapped component. Create a custom hook that takes a ref and a …
- React useComponentDidMount hook Executes a callback immediately after a component is mounted. Use the useEffect() hook with an empty array as …
- React useComponentDidUpdate hook Executes a callback immediately after a component is updated. Use the useRef() hook to create a variable, …
- React useComponentWillUnmount hook Executes a callback immediately before a component is unmounted and destroyed. Use the useEffect() hook with …
- React useCopyToClipboard hook Copies the given text to the clipboard. Use the copyToClipboard snippet to copy the text to clipboard. Use the …
- React useDebounce hook Debounces the given value. Create a custom hook that takes a value and a delay. Use the useState() hook to …
- React useDefault hook Creates a stateful value with a default fallback if it’s null or undefined, and a function to update it. …
- React useDelayedState hook Delays creating a stateful value until some condition is met. Use the useState() hook to create a stateful …
- React useEffectOnce hook Runs a callback at most once when a condition becomes true. Use the useRef() hook to create a variable, …
- React useError hook Creates an error dispatcher. Use the useState() hook to create a state variable that holds the error. Use the …
- React useEventListener hook Adds an event listener for the specified event type on the given element. Use the useRef() hook to create a …
- React useFetch hook Implements fetch() in a declarative manner. Create a custom hook that takes a url and options. Use the …
- React useForm hook Creates a stateful value from the fields in a form. Use the useState() hook to create a state variable for the …
- React useGetSet hook Creates a stateful value, returning a getter and a setter function. Use the useRef() hook to create a ref that …
- React useHash hook Tracks the browser’s location hash value, and allows changing it. Use the useState() hook to lazily get …
- React useHover hook Handles the event of hovering over the wrapped component. Use the useState() hook to create a variable that …
- React useIntersectionObserver hook Observes visibility changes for a given element. Use the useState() hook to store the intersection value of …
- React useInterval hook Implements setInterval() in a declarative manner. Create a custom hook that takes a callback and a delay. Use …
- React useIsomporphicEffect hook Eesolves to useEffect() on the server and useLayoutEffect() on the client. Use typeof to check if the Window …
- React useKeyPress hook Listens for changes in the pressed state of a given key. Use the useState() hook to create a state variable …
- React useLocalStorage hook Creates a stateful value that is persisted to localStorage, and a function to update it. Use the useState() …
- React useMap hook Creates a stateful Map object, and a set of functions to manipulate it. Use the useState() hook and the Map …
- React useMediaQuery hook Checks if the current environment matches a given media query and returns the appropriate value. Check if …
- React useMergeState hook Creates a stateful value, and a function to update it by merging the new state provided. Use the useState() …
- React useMutationObserver hook Watches for changes made to the DOM tree, using a MutationObserver Use a useEffect() hook that depends on the …
- React useNavigatorOnLine hook Checks if the client is online or offline. Create a function, getOnLineStatus, that uses the Navigator.onLine …
- React useOnGlobalEvent hook Executes a callback whenever an event occurs on the global object. Use the useRef() hook to create a variable, …
- React useOnWindowResize hook Executes a callback whenever the window is resized. Use the useRef() hook to create a variable, listener, …
- React useOnWindowScroll hook Executes a callback whenever the window is scrolled. Use the useRef() hook to create a variable, listener, …
- React usePersistedState hook Returns a stateful value, persisted in localStorage, and a function to update it. Use the useState() hook to …
- React usePortal hook Creates a portal, allowing rendering of children outside the parent component. Use the useState() hook to …
- React usePrevious hook Stores the previous state or props. Create a custom hook that takes a value. Use the useRef() hook to create a …
- React useRequestAnimationFrame hook Runs an animating function, calling it before every repaint. Use the useRef() hook to create two variables. …
- React useScript hook Dynamically loads an external script. Use the useState() hook to create a state variable for the load status …
- React useSearchParam hook Tracks the browser’s location search param. Use the useCallback() hook to create a callback that uses …
- React useSessionStorage hook Creates a stateful value that is persisted to sessionStorage, and a function to update it. Use the useState() …
- React useSet hook Creates a stateful Set object, and a set of functions to manipulate it. Use the useState() hook and the Set …
- React useSSR hook Checks if the code is running on the browser or the server. Create a custom hook that returns an appropriate …
- React useTimeout hook Implements setTimeout() in a declarative manner. Create a custom hook that takes a callback and a delay. Use …
- React useTitle hook Sets the title of the page Use typeof to determine if the Document is defined or not. Use the useRef() hook to …
- React useToggler hook Provides a boolean state variable that can be toggled between its two states. Use the useState() hook to …
- React useUnload hook Handles the beforeunload window event. Use the useRef() hook to create a ref for the callback function, fn. …
- React useUpdate hook Forces the component to re-render when called. Use the useReducer() hook that creates a new object every time …
- React useWindowSize hook Tracks the dimensions of the browser window. Use the useState() hook to initialize a state variable that will …
- Show/hide password toggle Renders a password input field with a reveal button. Use the useState() hook to create the shown state …
- Spinning loader Renders a spinning loader component. Render an SVG, whose height and width are determined by the size prop. …
- Star rating Renders a star rating component. Define a Star component. It renders each individual star with the appropriate …
- Stateful checkbox with multiple selection Renders a checkbox list that uses a callback function to pass its selected value/values to the parent …
- Tabs Renders a tabbed menu and view component. Define a Tabs component. Use the useState() hook to initialize the …
- Tag input field Renders a tag input field. Define a TagInput component and use the useState() hook to initialize an array from …
- Textarea with character limit Renders a textarea component with a character limit. Use the useState() hook to create the content state …
- Textarea with word limit Renders a textarea component with a word limit. Use the useState() hook to create a state variable, containing …
- Toggle Renders a toggle component. Use the useState() hook to initialize the isToggledOn state variable to …
- Tooltip Renders a tooltip component. Use the useState() hook to create the show variable and initialize it to false. …
- Uncontrolled input field Renders an uncontrolled <input> element that uses a callback function to inform its parent about value …
- Uncontrolled range input Renders an uncontrolled range input element that uses a callback function to pass its value to the parent …
- Uncontrolled select element Renders an uncontrolled <select> element that uses a callback function to pass its value to the parent …
- Uncontrolled textarea element Renders an uncontrolled <textarea> element that uses a callback function to pass its value to the parent …
No snippet matches that filter.