React Reference ยท Components and Special Elements
React function components
Reference for React function components: naming, props, return values, hooks, why not to define components inside components, and React 19 changes.
A function component is a JavaScript function that takes a single props object and returns something React can render, usually JSX. It is the standard way to write components: short, easy to test, and the only kind that can use hooks.
React decides that something is a component from how you use it. A name that starts with a capital letter in JSX (<Card />) is a component; a lowercase name (<card />) is treated as a built-in HTML tag. The function must be pure while rendering: given the same props, state and context, it returns the same output and changes nothing outside itself.
function Welcome({ name }) { return <h1>Welcome, {name}</h1>; }
Try it
Type into both fields, then press the button. "Safe field" keeps its text. "Broken field" loses it, because InnerField is a different function each time Form runs, so React sees a different component type, throws away the old one and mounts a new one with empty state.
Syntax
function Name(props) { return /* renderable value */; } const Name = ({ a, b = 1, ...rest }) => /* renderable value */;
| Part | Description |
|---|---|
| Name | Must start with an uppercase letter to be used as <Name />. |
props |
One object holding all attributes plus children. Treat it as read-only. Destructure it in the parameter list to get defaults. |
| Return value | Any renderable value: an element, string, number, array, fragment, portal, null, undefined, or false. Functions and plain objects are not valid children. |
Errors: returning an object that is not an element (for example return { a: 1 }) throws "Objects are not valid as a React child". Calling a hook conditionally or in a loop breaks the rules of hooks.
Usage notes
- Hooks. Only function components (and custom hooks) can call
useState,useEffectand the rest, and only at the top level, never in conditions or loops. - Default props. Use default parameter values:
function Button({ size = 'md' }). In React 19 the staticComponent.defaultPropswas removed for function components, as waspropTypeschecking; use TypeScript or runtime checks you write yourself. - Refs are props in React 19. A function component receives
refas a normal prop;forwardRefis no longer required. - Arrow or declaration. Both are fine. Declarations are hoisted and get a clear name in DevTools and stack traces.
- Keep rendering pure. No mutation of props, no changing outer variables, no network calls. Do side effects in event handlers or
useEffect. - Define components at the top level of a module. Defining one inside another resets its state on every render of the parent (see the second example) and defeats memoization.
- Don't call components as functions. Write
<Card />, notCard(). Calling it directly runs its hooks as if they belonged to the parent. - No
this. There are no instance fields; use state or refs for anything that persists between renders. - Re-rendering. A component renders again when its state changes, its parent renders it with new props, or a context it reads changes. Wrapping it in
memoskips the middle case when props are equal.
Related
- Component, Fragment, useState, ref as a prop
- Lessons: Components, Props, Rules of hooks