October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

How to Extend Function Components in React

React function components cannot extend one another with extends. Use custom Hooks to reuse behavior, composition to reuse UI, and context for values needed deep in a component tree.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

You can’t extend one React function component from another with extends. Instead, choose the reuse pattern that fits what you want to share: use a custom Hook for behavior, component composition for UI, or context for values needed by distant descendants. JavaScript class inheritance applies to React class components, not function components.

Choose the pattern for what you want to reuse

What you need Use What it changes
Shared stateful or React-specific logic Custom Hook Shares logic between components; each component calls the Hook and maintains its own Hook state.
Shared or variable visual structure Composition with props, children, or component-valued props Builds the rendered tree from reusable components and caller-supplied UI.
A value needed by many descendants Context with useContext Makes a value available to descendants without passing it through every intermediate component.
Existing class-component code Class inheritance where needed Allows a class to extend React’s Component; it does not make function components inheritable.

React describes custom Hooks and composition as ways to reuse logic and UI. Its Component reference demonstrates class components extending Component and recommends defining new components as functions.

Reuse behavior with a custom Hook

Move reusable React logic into a function whose name begins with use. A Hook can call built-in Hooks or other custom Hooks, then return state, derived values, and event handlers for a component to use. Each component calls the Hook where it needs the behavior; the Hook does not automatically create shared global state. See React’s guide to reusing logic with custom Hooks.

import { useState } from 'react';

function useCounter(initialValue = 0) {
  const [count, setCount] = useState(initialValue);

  function increment() {
    setCount(value => value + 1);
  }

  return { count, increment };
}

function CounterButton() {
  const { count, increment } = useCounter();
  return <button onClick={increment}>Count: {count}</button>;
}

function CounterLabel() {
  const { count } = useCounter();
  return <p>Count: {count}</p>;
}

In this example, the two components use the same counter logic, but each call to useCounter creates state for that component. To make multiple components read or update the same state, place ownership in a shared parent and pass values and handlers down, provide them through context, or use an appropriate external store.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Reuse and vary UI with composition

When the thing to share is markup or layout, compose components rather than trying to inherit a component’s implementation. Pass changing values and behavior as props. Use children when callers should supply nested content, or a component-valued prop when they should supply a particular part of the UI.

function Panel({ title, children }) {
  return (
    <section className="panel">
      <h2>{title}</h2>
      <div>{children}</div>
    </section>
  );
}

function AccountPage() {
  return (
    <Panel title="Account">
      <AccountDetails />
    </Panel>
  );
}

Panel supplies structure while its caller chooses the nested content. For information needed deep in a component tree, context can avoid threading the same prop through components that do not use it; React’s built-in Hooks reference includes useContext.

When class inheritance is relevant

In React, the inheritance form is class syntax: a class component can extend React’s Component class. That is distinct from function components, which are functions and do not inherit from one another with extends. React’s class-component reference also states that Hooks do not work inside class components. Classes may remain relevant in existing code or where an API requires them; this is not a reason to rewrite every existing class.

Why a higher-order component is not inheritance

A higher-order component (HOC) is a function that takes a component and returns another component, commonly to wrap it with additional behavior. It may be useful when a codebase or library already uses that pattern, but it is not class inheritance and does not make one function component a subclass of another. For reusable React logic in function components, a custom Hook is generally the more direct choice when it meets the need.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Rules that keep the patterns working

  • Call Hooks only at the top level. Do not call them conditionally, in loops, inside nested callbacks, or from ordinary JavaScript functions. A component or custom Hook can call them at its top level. See React’s Rules of React.
  • Render components through JSX. Use <MyComponent /> rather than calling a component function as an ordinary helper. React needs to control when components render and track their Hooks.
  • Keep render pure. Do not perform side effects during render or mutate props and state. React’s purity guidance says, “Side effects must run outside of render.” Put external-system synchronization in an appropriate effect and user-triggered work in an event handler.
  • Do not use memo as an extension mechanism. React’s memo reference describes it as a performance optimization that can skip rendering when props are unchanged by shallow comparison. It neither adds behavior nor creates inheritance, and new object or function references can affect that comparison.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Decide where shared state belongs

A custom Hook reuses a recipe for behavior, not a single state instance. If several components must observe the same changing value, identify its owner: a common parent can hold state and pass it through props; context can make it available to descendants; or an external store can own it when appropriate. Choose based on the component tree and who needs to read or update the value, rather than expecting a Hook by itself to synchronize separate calls.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.