Do these 3 things before closing this tab:
1Fix the driver behind crashes, sound loss and screen glitches2Repair Windows errors before they cause bigger problems3Scan for outdated or missing drivers - takes under a minuteYou 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.
#1 Best Overall
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.
Rank #3
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
memoas 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.
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.
Quick Recap
Best Value
Rank #4
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.




