
useState give us a way to declare stateful values along with a setter function to update that state.useState hook to declare a piece of state called count and set its initial value to 0. The setCount function allows us to update this state. Every time the button is clicked, we use setCount to increase count by 1. When the state updates, React re-renders the component to reflect the change.let count = 0, using useState lets React remember the state across renders and ensures that the UI updates correctly.use keyword – even custom hooks start with this structure. This keyword is reserved for Hooks in React.useStateuseState hook is the bread and butter of React. It’s the most commonly used hook, and it’s key for managing state in functional components. With useState, you can capture user inputs, show or hide components, and manage numbers, like in an ecommerce app with a shopping cart.useState is versatile and straightforward: you initialize it with a value, and it returns a state variable and an updater function.useState initializes the state (count) and provides a function (setCount) to update that state.useReduceruseState isn’t enough, useReducer comes into play. This hook is perfect for managing complex state logic.useReducer is useful for managing complex state updates, like handling multiple related actions.useSyncExternalStore
useSyncExternalStore is a hook for integrating non-React state stores into your React components.useSyncExternalStore lets you connect your React component to non-React data sources, like global stores.useEffect
The useEffect hook performs side effects on your components. Whether you’re interacting with the DOM or fetching data, useEffect is your go-to. It runs after each render by default, but you can customize its behavior using a dependency array.useEffect hook fetches data when the component mounts. The effect will only run one time when the array is empty.useLayoutEffect
useLayoutEffect works similarly to useEffect but runs synchronously right after the DOM has been updated. It’s used for operations that need to happen before the browser paints the UI, like measuring elements.useEffect. Here’s an example:useLayoutEffect measures DOM elements before the browser repaints.useInsertionEffect
Exclusively for CSS-in-JS library developers, useInsertionEffect runs before useEffect and useLayoutEffect to ensure that CSS styles are inserted properly. It’s niche, but crucial for maintaining style integrity in complex applications.useInsertionEffect hook is used to inject styles into the DOM at runtime, making the component’s styling dynamic and scoped only to that component.useRef
useRef allows you to persist values across renders without causing re-renders. It's perfect for storing mutable values or referencing DOM elements. Whether you’re handling intervals, storing a DOM node, or keeping track of the previous state, useRef has you covered.useRef to create a reference to an input element. When the button is clicked, the handleFocus function triggers the input field to gain focus using inputRef.current.focus().useMemo
For optimizing performance, useMemo is your friend. It caches the results of expensive computations and only recalculates when dependencies change. This can significantly improve performance, especially in scenarios involving heavy calculations.useMemo to optimize an expensive calculation (count * 100). The calculation only re-runs when count changes. The button increments count, triggering a UI update with the new result.useCallback
useCallback is similar to useMemo, but it focuses on memoizing callback functions. This is useful for preventing unnecessary re-renders of child components by keeping functions stable across renders.useCallback to memoize the handleClick function, preventing re-creation on every render. The Child component uses this function for its button. The parent updates count independently.useContext
The useContext hook simplifies accessing context values. It reads the value from the nearest context provider and works seamlessly across nested components. This makes it easier to manage global states or themes.createContext to create a ThemeContext. useContext accesses the context value, displaying it in the button. The App component provides "dark" as the theme to ThemedButton.useTransition
useTransition lets you mark specific state updates as low-priority, enhancing the user experience by keeping the app more responsive during intensive computations or transitions. This improves the user experience by making the app more responsive.useTransition to increment count without blocking the UI. While the state updates, isPending shows "Loading...". Clicking the button triggers a smooth, non-blocking state transition.useDeferredValue
Similar to useTransition, useDeferredValue helps in deferring state updates to keep the app responsive. It schedules updates to happen at an optimal time, enhancing the user experience without manual intervention.useDeferredValue delays the update of deferredValue to ensure that the UI remains responsive.useDebugValue
useDebugValue is a hook primarily for debugging. It lets you label custom hooks in React DevTools, making it easier to track and debug your hooks.useDebugValue to show "Has Value" or "No Value" in React DevTools based on value. useCustomHook is used in DebugComponent to track the input state and display it dynamically.useId
useId generates unique IDs for elements, ensuring that form inputs and labels are properly linked without conflicts. It’s particularly useful when dealing with dynamically repeated elements.useId ensures that form elements have unique IDs, avoiding potential conflicts.Posted Oct 3, 2025
Educational article on React Hooks for freeCodeCamp.