### Install Dependencies Source: https://github.com/uidotdev/usehooks/blob/main/usehooks.com/README.md Installs all the necessary dependencies for the project. ```bash npm install ``` -------------------------------- ### Start Local Development Server Source: https://github.com/uidotdev/usehooks/blob/main/usehooks.com/README.md Starts a local development server, typically accessible at localhost:3000. ```bash npm run dev ``` -------------------------------- ### Basic useFetch Example Source: https://github.com/uidotdev/usehooks/blob/main/usehooks.com/src/content/hooks/useFetch.mdx Demonstrates how to use the useFetch hook to fetch data from a URL and display it, handling loading and error states. This example fetches Pokemon data based on a counter. ```jsx import * as React from "react"; import { useFetch } from "@uidotdev/usehooks"; import Card from "./Card"; export default function App() { const [count, setCount] = React.useState(1); const { error, data } = useFetch( `https://pokeapi.co/api/v2/pokemon/${count}` ); return (

useFetch

); } ``` -------------------------------- ### Basic useTimeout Example Source: https://github.com/uidotdev/usehooks/blob/main/usehooks.com/src/content/hooks/useTimeout.mdx This example demonstrates how to use the useTimeout hook to trigger an action after a specified delay. The returned function can be used to clear the timeout. ```jsx import * as React from "react"; import { useTimeout } from "@uidotdev/usehooks"; function Bomb({ hasExploded, hasDefused, handleClick }) { if (hasExploded) { return (
๐Ÿ’ฅ
You lose
); } if (hasDefused) { return (
๐ŸŽ‰
You Win
); } return ( ); } export default function App() { const [hasDefused, setHasDefused] = React.useState(false); const [hasExploded, setHasExploded] = React.useState(false); const clear = useTimeout(() => { setHasExploded(!hasExploded); }, 1000); const handleClick = () => { clear(); setHasDefused(true); }; return (

useTimeout

You have 1s to defuse (click) the bomb or it will explode

); } ``` -------------------------------- ### Basic useHover Example Source: https://github.com/uidotdev/usehooks/blob/main/usehooks.com/src/content/hooks/useHover.mdx Demonstrates how to use the useHover hook to track hover state and change background color accordingly. Attach the returned ref to the element you want to monitor. ```jsx import * as React from "react"; import { useHover } from "@uidotdev/usehooks"; function getRandomColor() { const colors = ["green", "blue", "purple", "red", "pink"]; return colors[Math.floor(Math.random() * colors.length)]; } export default function App() { const [ref, hovering] = useHover(); const backgroundColor = hovering ? `var(--${getRandomColor()})` : "var(--charcoal)"; return (

useHover

Hovering? {hovering ? "Yes" : "No"}
); } ``` -------------------------------- ### Get Astro CLI Help Source: https://github.com/uidotdev/usehooks/blob/main/usehooks.com/README.md Displays help information for the Astro CLI. ```bash npm run astro --help ``` -------------------------------- ### Basic useIntervalWhen Example Source: https://github.com/uidotdev/usehooks/blob/main/usehooks.com/src/content/hooks/useIntervalWhen.mdx This example demonstrates how to use useIntervalWhen to create a timer that increments a counter. The timer starts when the button is clicked and stops when clicked again. The 'startImmediately' option is set to true, so the timer begins as soon as the 'when' condition becomes true. ```jsx import * as React from "react"; import { useIntervalWhen } from "@uidotdev/usehooks"; export default function App() { const [count, setCount] = React.useState(0); const [when, setWhen] = React.useState(false); useIntervalWhen( () => { setCount((c) => c + 0.1); }, { ms: 100, when, startImmediately: true } ); return (

useIntervalWhen

); } ``` -------------------------------- ### useInterval Example Source: https://github.com/uidotdev/usehooks/blob/main/usehooks.com/src/content/hooks/useInterval.mdx This example demonstrates how to use the useInterval hook to change a background color every second. It also shows how to stop the interval using the returned clearInterval function. ```jsx import * as React from "react"; import { useInterval } from "@uidotdev/usehooks"; const colors = ["green", "blue", "purple", "red", "pink", "beige", "yellow"]; export default function App() { const [running, setIsRunning] = React.useState(true); const [index, setIndex] = React.useState(0); const clear = useInterval(() => { setIndex(index + 1); }, 1000); const handleStop = () => { clear(); setIsRunning(false); }; const color = colors[index % colors.length]; return (

useInterval

); } ``` -------------------------------- ### React useQueue Hook Example Source: https://github.com/uidotdev/usehooks/blob/main/usehooks.com/src/content/hooks/useQueue.mdx Demonstrates the usage of the useQueue hook to manage a queue of numbers. Includes buttons to add, remove, and clear elements, and displays the current queue state. ```jsx import * as React from "react"; import { useQueue } from "@uidotdev/usehooks"; function QueueDemo({ first, last, size, queue }) { return (

Front

Back

{size} items in the queue
); } export default function App() { const { add, remove, clear, first, last, size, queue } = useQueue([1, 2, 3]); return (

UseQueue

); } ``` -------------------------------- ### useMeasure Hook Example Source: https://github.com/uidotdev/usehooks/blob/main/usehooks.com/src/content/hooks/useMeasure.mdx This example demonstrates how to use the useMeasure hook to track the width or height of an element. Attach the returned ref to the element you want to measure. The hook provides the dimensions in the rect object. ```jsx import * as React from "react"; import { useMeasure } from "@uidotdev/usehooks"; function Measure({ type = "horizontal" }) { const [ref, { width, height }] = useMeasure(); return (
{type}
{type === "horizontal" ? ( ) : ( )}
); } export default function App() { return (

useMeasure

(Resize the rulers)

); } ``` -------------------------------- ### useClickAway Hook Example Source: https://github.com/uidotdev/usehooks/blob/main/usehooks.com/src/content/hooks/useClickAway.mdx This example demonstrates how to use the useClickAway hook to close a modal when a user clicks outside of it. Attach the returned ref to the dialog element. ```jsx import * as React from "react"; import { useClickAway } from "@uidotdev/usehooks"; import { closeIcon } from "./icons"; export default function App() { const [isOpen, setIsOpen] = React.useState(false); const ref = useClickAway(() => { setIsOpen(false); }); const handleOpenModal = () => { if (isOpen === false) { setIsOpen(true); } }; return ( <>

useClickAway

{isOpen && (

Modal

Click outside the modal to close (or use the button) whatever you prefer.

)} ); } ``` -------------------------------- ### Basic useCounter with Min/Max Limits Source: https://github.com/uidotdev/usehooks/blob/main/usehooks.com/src/content/hooks/useCounter.mdx Demonstrates initializing useCounter with a starting value and defining minimum and maximum bounds. Includes buttons to increment, decrement, set to a specific value, and reset the counter, with the current count displayed. ```jsx import * as React from "react"; import { useCounter } from "@uidotdev/usehooks"; export default function App() { const [count, { increment, decrement, set, reset }] = useCounter(5, { min: 5, max: 10, }); return (

UseCounter

with optional min / max

{count}

); } ``` -------------------------------- ### Basic useIntersectionObserver Example Source: https://github.com/uidotdev/usehooks/blob/main/usehooks.com/src/content/hooks/useIntersectionObserver.mdx Demonstrates how to use the useIntersectionObserver hook to conditionally render content when an element becomes visible in the viewport. It utilizes default parameters for threshold, root, and rootMargin. ```jsx import * as React from "react"; import { useIntersectionObserver } from "@uidotdev/usehooks"; import demoData from "./demoData"; const Section = ({ imgUrl, caption, href }) => { const [ref, entry] = useIntersectionObserver({ threshold: 0, root: null, rootMargin: "0px", }); return (
{entry?.isIntersecting && ( <> {caption}
{caption}
)}
); }; export default function App() { return (

useIntersectionObserver

(Memes from bytes.dev)
{demoData.map(({ imgUrl, href, caption }, index) => { return (
); })}
); } ``` -------------------------------- ### useSet Hook Example Source: https://github.com/uidotdev/usehooks/blob/main/usehooks.com/src/content/hooks/useSet.mdx Demonstrates how to use the useSet hook to manage a list of usernames, check for availability, and add new usernames. It includes form handling and conditional styling based on username availability. ```jsx import * as React from "react"; import { useSet } from "@uidotdev/usehooks"; function format(val) { return val.toLocaleLowerCase().replace(/\s/g, ""); } export default function App() { const [value, setValue] = React.useState(""); const set = useSet([ "benadam11", "tylermcginnis", "lynnandtonic", "alexbrown40", "uidotdev", "bytesdotdev", "reactnewsletter", ]); const handleSubmit = (e) => { e.preventDefault(); const formData = new FormData(e.target); const username = formData.get("username"); set.add(format(username)); setValue(""); e.target.reset(); e.target.focus(); }; const hasError = set.has(value); return (

useSet

Check if your username is available

@ { setValue(format(e.target.value)); }} />
{hasError && }
{Array.from(set.values()).map((username) => { const match = username === value; return ( ); })}
username {username}
); } ``` -------------------------------- ### React useHistoryState Example Source: https://github.com/uidotdev/usehooks/blob/main/usehooks.com/src/content/hooks/useHistoryState.mdx Demonstrates how to use the useHistoryState hook to manage a list of items with undo, redo, and clear functionality. It includes functions to add and remove items from the state. ```jsx import * as React from "react"; import Form from "./Form"; import { useHistoryState } from "@uidotdev/usehooks"; export default function App() { const { state, set, undo, redo, clear, canUndo, canRedo } = useHistoryState({ items: [], }); const addTodo = (val) => { set({ ...state, items: state.items.concat({ id: crypto.randomUUID(), name: val }), }); }; const removeTodo = (id) => { set({ ...state, items: state.items.filter((item) => item.id !== id), }); }; return (

useHistoryState

); } ``` -------------------------------- ### React useCopyToClipboard Example Source: https://github.com/uidotdev/usehooks/blob/main/usehooks.com/src/content/hooks/useCopyToClipboard.mdx Demonstrates how to use the useCopyToClipboard hook in a React component to copy a generated API key. It shows how to conditionally render UI elements based on whether text has been copied. ```jsx import * as React from "react"; import { useCopyToClipboard } from "@uidotdev/usehooks"; import { copyIcon, checkIcon } from "./icons"; const randomHash = crypto.randomUUID(); export default function App() { const [copiedText, copyToClipboard] = useCopyToClipboard(); const hasCopiedText = Boolean(copiedText); return (

useCopyToClipboard

          {randomHash}
          
        
{hasCopiedText && (

Copied{" "} ๐ŸŽ‰