October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk5 min

React Function Components with TypeScript: Props, Children, Hooks, and Types

A practical guide to React function components in TypeScript: configure JSX, type props and children, handle events, use Hooks, and avoid state mutation.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Define a React function component by giving its props an object type, then accept that type as the function’s parameter. TypeScript infers many values from context and initial values, so add annotations where they clarify the component’s intended API or inference is insufficient. This guide covers the setup, prop and child types, event handlers, Hooks, and safe state updates.

Set up TypeScript for React JSX

Use the .tsx extension for each TypeScript file that contains JSX. For React web type definitions, install @types/react and @types/react-dom. The TypeScript configuration also needs DOM library types; dom is included by default if lib is omitted. Set jsx to a valid compiler option; preserve is sufficient for many applications. See the React TypeScript guide and, for published libraries, the TypeScript JSX configuration documentation.

As an Amazon Associate I earn from qualifying purchases.

There is no single setup command that fits every React framework. Follow the current installation guide for the framework you use, then check that its TypeScript configuration covers the JSX and DOM requirements.

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

Type props on a function component

A component’s props are an object-shaped input contract. Define that shape with either a type alias or an interface, and use it as the function parameter type:

interface GreetingProps {
  name: string;
  excited?: boolean;
}

function Greeting({ name, excited = false }: GreetingProps) {
  return <h1>{excited ? `Hello, ${name}!` : `Hello, ${name}`}</h1>;
}

Here, name is required and excited is optional. The default value in the parameter list is ordinary JavaScript destructuring; it does not replace the props type. A type alias describes this same object shape equally well. Choose the form that fits your team’s conventions and how you need to compose or extend the type.

Inline or named props?

An inline object annotation can be concise when a component has only a tiny, one-off prop shape. A named type or interface is easier to scan when props include several fields, need reuse, or benefit from a descriptive name. This is an API clarity choice, not a performance optimization.

Rank #2
TypeScript Programming Language - Software Engineer & Coder T-Shirt
  • TypeScript implements a superset of syntax for strictly typed development, facilitating deep static analysis and enhanced development environment integration. The compiler translates source into standard script formats, ensuring parity across any runtime.
  • TypeScript is ideal for front-end developers, full-stack engineers, and software architects who build large-scale web applications. It serves those looking to improve code excellence, reduce bugs through static checking, and maintain complex projects more.
  • Lightweight, Classic fit, Double-needle sleeve and bottom hem

Represent limited alternatives with unions

When a prop may take only specific values, a union makes those alternatives explicit—for example, type Status = "loading" | "success" | "error". Consumers then get type checking against the declared options rather than an unrestricted string.

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

Choose a type for children

Pick the children type based on what the component is designed to accept. React.ReactNode is broad: it covers the range of values React accepts as JSX children, including text and numbers. React.ReactElement is narrower and represents a JSX element, not primitive text or numeric children.

interface PanelProps {
  children: React.ReactNode;
}

function Panel({ children }: PanelProps) {
  return <section>{children}</section>;
}

TypeScript cannot use these types to require that children be a particular JSX tag, such as requiring only <li> children. The type describes the value category, not a specific tag name.

Type event handlers and style props

When a handler is written inline in JSX, TypeScript can often infer its event type from the element and callback position. If you extract the handler into a separate function, annotate its parameter when inference does not provide the type you need. For an input change event, React’s guide uses React.ChangeEvent<HTMLInputElement>:

function handleChange(event: React.ChangeEvent<HTMLInputElement>) {
  console.log(event.currentTarget.value);
}

React also provides event-handler aliases, such as React.ChangeEventHandler<HTMLInputElement>. For less common event types, React.SyntheticEvent is the base event type. For a prop that accepts an inline style object, use React.CSSProperties.

Free tools Windows power users keep installed

One-click scans. No signup required.

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

Let TypeScript infer Hook types where it can

React’s Hook type definitions work with TypeScript inference. For example, useState(false) infers a boolean state value and a setter that accepts booleans. Context takes its value type from the value passed to createContext or from an explicit generic. A memoized value is inferred from the return value of its callback.

Add an explicit type argument or annotation when the initial value does not express the full state domain or inference misses your intent. For example, a state initialized to null may later hold an object, so declare that wider domain rather than letting the initializer define the whole type:

const [user, setUser] = useState<User | null>(null);

In strict TypeScript, a callback parameter passed to useCallback may need an explicit type. A React handler alias can express the input event and element together:

const handleChange: React.ChangeEventHandler<HTMLInputElement> = (event) => {
  setQuery(event.currentTarget.value);
};
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Call Hooks only at the top level

Hooks are not general-purpose functions that can be called wherever convenient. Call them only at the top level of a function component or custom Hook, before any early return. Do not call Hooks in conditions, loops, nested functions, event handlers, try/catch/finally blocks, or class components. React’s Rules of Hooks explain the restrictions; the eslint-plugin-react-hooks plugin can help catch violations.

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

Keep rendering pure and update state through its setter

A component should render from its inputs without changing the outside world: the same props and state should produce the same output, while side effects belong outside render. Treat props and state as immutable snapshots. Do not change an object received through props or directly assign to a state variable. Instead, derive a copy when needed and use the setter returned by useState to request an update. Directly changing a local state variable does not notify React to render again. See React’s purity rules.

Function components and class components

React’s documentation says, “We recommend defining components as functions instead of classes.” Class components remain documented and supported, but they receive props through this.props; function components receive props as parameters. Hooks cannot be used inside class components. See the official Component reference.

Common typing choices at a glance

Decision Use this when What it communicates
Inline props object The prop shape is small and used once A concise input contract
Named type or interface The component has several props, the shape is reused, or it needs a clear name A reusable, documented input contract
React.ReactNode The component accepts broad JSX child values, including text or numbers A broad children API
React.ReactElement The component requires a JSX element rather than primitive children A narrower children API
Inferred Hook type The initializer or callback return already expresses the intended type The type follows from the value
Explicit Hook type or annotation The intended domain is broader than the initializer or callback inference is insufficient An explicit state or callback contract

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
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

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.