Components, JSX & props
Break a UI mockup into a tree of reusable components.
By Invicly Tech, , in Frontend with React & Next.js. About 11 minutes to read.
In short
React apps are built from components: functions that take props and return JSX describing UI. Small components compose into a tree, which makes UIs easier to reuse and reason about.
Think of it like this: Components are like LEGO bricks that snap together into bigger pieces.
Explained simply
A web page has many parts: a header, a search bar, a list of products, a footer. React lets you build each part as its own small piece of code, called a component. Then you put the pieces together, like building a house from bricks.
A component is just a JavaScript function. It returns a description of what should appear on screen, written in JSX, which looks like HTML inside JavaScript. React reads that description and updates the real page for you.
Components take inputs called props, short for properties. A ProductCard might take a title and a price. The same component can show a mug or a T-shirt, depending on the props you give it. Props go one way only: from the parent down to the child.
The skill that matters most is splitting a design into components. Draw boxes around each part of the mockup. If a box repeats, or does one clear job, it is probably a component. Good components are small, focused and easy to reuse.
Press Play on a diagram to walk through it one step at a time.
- Header
- Logo
- SearchBar
- CartButton
- ProductList
- ProductCard
- ProductCard
- ProductCard
- Footer
- Passed in by the parent
- Read-only for the child
- Change when the parent re-renders
- Like function arguments
- Content between the tags
- Chosen by the caller
- Lets wrappers stay generic
- Typed as ReactNode
- Owned by the component itself
- Changed with a setter
- Survives re-renders
- Covered in the next topic
In depth
What JSX turns into
Browsers do not understand JSX. A compiler (SWC in Next.js, esbuild or Babel elsewhere) turns each tag into a function call. With the modern JSX transform, an h2 with some text becomes a call to jsx("h2", { children: title }), imported automatically from react/jsx-runtime. The result is a plain JavaScript object called a React element, which describes what you want, not the real DOM node.
This explains the JSX rules. Curly braces hold any JavaScript expression, but not statements like if or for. A component must return one root, or a Fragment to group siblings without an extra div. Attributes use JavaScript names: className instead of class, htmlFor instead of for, onClick in camelCase, and style takes an object such as style={{ marginTop: 8 }}.
- 1JSX in a .tsx file
Looks like HTML with curly-brace values
- 2Compiler
Turns tags into jsx() function calls
- 3React elements
Plain objects describing the UI
- 4React DOM
Compares with the last render
- 5Real DOM
Only the changed nodes are updated
Props in practice
Props are a single object passed to your function, so you usually destructure them: function Avatar({ name, size = 40 }). Default values cover optional props. Props are read-only. If a child needs to change something, the parent passes down a function, such as onSelect, and the child calls it.
For wrapper components, accept the native props too. Typing props as ComponentProps of "button" plus your own fields lets callers pass disabled, type or aria-label, and you forward them with a rest spread. In React 19, ref is a normal prop for function components, so a wrapper can receive and pass a ref without the old forwardRef helper. The key prop is special: React uses it and does not pass it to your component.
- Destructure props and give defaults
- Pass functions down so children can ask for changes
- Forward native props with ...rest
- React 19: ref is a regular prop
children and composition
Anything you put between a component's opening and closing tags arrives as the children prop. This makes layout components easy: a Card can draw the border and padding while the caller decides what goes inside. Typed as ReactNode, children accepts text, elements, arrays, or nothing.
When you need more than one hole, use named slot props: a Dialog with title, children and footer props, each a ReactNode. Composition often removes the need for deep prop passing. Instead of passing user through Layout and Sidebar just so Avatar can use it, the page renders Layout with the Avatar already inside it as children. The middle components never need to know about user.
Breaking a mockup into components
The React docs call this Thinking in React. First, draw boxes around every part of the design and name them. A good rule is one component, one job. Second, arrange the boxes into a hierarchy: boxes inside other boxes become children. Third, build a static version that only renders props, with no interactivity. This is quick and shows whether your split works.
Only then decide which data changes over time and must be state, and which component should own it. Look at the shape of your data too: if the API returns categories, each with products, a CategorySection containing ProductRows probably mirrors it nicely.
- 1Draw boxes
One box per distinct or repeated part
- 2Name and nest
Inner boxes become child components
- 3Static version
Render from props only, no state
- 4Find the state
What changes over time?
- 5Place the state
Closest common parent of its users
Components must be pure
React expects components to behave like pure functions during rendering: the same props and state should always produce the same JSX, and rendering must not change anything outside the component. Do not edit global variables, mutate props, start network requests or write to localStorage in the function body.
React relies on this. It may render a component more than once, pause a render, or throw one away. In development, Strict Mode renders every component twice on purpose so that impure code shows up as odd behavior early. Side effects belong in event handlers, which run because the user did something, or in effects, which run after rendering.
Learn it in 4 short steps
About 1h each. Track them with a timer in your plan.
- 1Write function components with JSX~1h
- 2Pass data with props~1h
- 3Render children and compose components~1h
- 4Break a mockup into a component tree~1h
Key points
- A component is a function whose name starts with a capital letter and returns JSX.
- Props flow down from parent to child and are read-only.
- The children prop lets a component wrap any nested content.
- Split a mockup into components by drawing boxes around repeated or distinct parts.
Key terms
Tap a card to flip it. 0/5 known
Examples
type CardProps = { title: string; price: number };
function ProductCard({ title, price }: CardProps) {
return (
<article className="card">
<h2>{title}</h2>
<p>${price.toFixed(2)}</p>
</article>
);
}
export default function App() {
return <ProductCard title="Mug" price={12} />;
}A typed component receiving props from its parent.
A Card with slots
Many screens need the same box with a title, a body and an optional footer. Step 1: make Card take title, children and an optional footer, all ReactNode. Step 2: Card only handles the frame and spacing. Step 3: each page fills the slots with its own content. The settings page and the billing page share one look, but neither Card nor its callers know about each other's data.
import type { ReactNode } from "react";
type CardProps = { title: ReactNode; children: ReactNode; footer?: ReactNode };
export function Card({ title, children, footer }: CardProps) {
return (
<section className="card">
<h2 className="card-title">{title}</h2>
<div className="card-body">{children}</div>
{footer && <footer className="card-footer">{footer}</footer>}
</section>
);
}
// <Card title="Plan" footer={<a href="/billing">Change</a>}>Pro, $12/month</Card>A Button that behaves like a real button
A custom Button should still accept everything a native button does. Step 1: type the props as the native button props plus a variant. Step 2: pull out the fields you handle and collect the rest. Step 3: spread the rest onto the button, so type, disabled, onClick, aria-label and even ref (a plain prop in React 19) just work. Callers never have to ask you to add another prop.
import type { ComponentProps } from "react";
type ButtonProps = ComponentProps<"button"> & {
variant?: "primary" | "ghost";
};
export function Button({ variant = "primary", className = "", ...rest }: ButtonProps) {
return <button type="button" className={`btn btn-${variant} ${className}`} {...rest} />;
}
// <Button variant="ghost" onClick={close} aria-label="Close">X</Button>Quick reference
- Component names start with a capital letter; lowercase tags are HTML elements
- Use className, htmlFor and camelCase event names like onClick in JSX
- Return one root element, or wrap siblings in a Fragment
- Props are read-only; pass a callback like onChange so children can request changes
- Type children as ReactNode to accept text, elements or nothing
- Spread ...rest to forward native props such as disabled and aria-label
- React 19: function components receive ref as a normal prop, no forwardRef needed
- Keep rendering pure: no fetches, timers or mutations in the component body
Common mistakes
- Changing props inside a child instead of asking the parent to update them.
- Building one giant component instead of splitting it into focused pieces.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Write a Badge component that takes a label and an optional tone of "info" or "danger" (default "info") and renders a span with a matching class.
- Exercise 2medium
Build a Layout component with a sidebar slot and children, then use it so a UserMenu inside the sidebar gets the user directly from the page, without Layout knowing about users.
- Exercise 3hard
Take a mockup of a recipe page (header, search, filter chips, a grid of recipe cards with title, time and tags, and pagination). Write the component tree and the props each component needs.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- What is the difference between a component and an element in React?
- A component is the function you write. An element is the small object it returns, describing what to show; React turns elements into real DOM nodes.
- Should I use default or named exports for components?
- Either works. Named exports are easier to auto-import and rename safely; Next.js requires default exports only for special files like page.tsx and layout.tsx.
- Do I still need forwardRef in React 19?
- No for new code. Function components can take ref as a normal prop; forwardRef still works but is no longer needed.
- How small should a React component be?
- Small enough to do one job and have a clear name. If you need the word and to describe it, consider splitting it.