Sub topic 1
Elements, Components, and JSX: What a Component Returns
Prerequisites
This chapter assumes you know the following:
- JavaScript functions, objects, and arrays
- Basic React components, props, and JSX syntax
- The
useStatehook - The browser DOM at a basic level (elements, attributes, children)
Later chapters build on the model introduced here, so it is worth getting the vocabulary right now.
Components return descriptions, not DOM nodes
A component is a function that takes props and returns a description of the UI. That description is made of elements. An element is a plain JavaScript object that says what you want on screen. It is not a DOM node, and creating one does not touch the page.
JSX is syntax that produces these objects. This code:
const heading = <h1 className="title">Hello</h1>;compiles to a call to createElement (or the equivalent jsx function from the compiler runtime). The call returns an object shaped roughly like this:
const heading = {
type: 'h1',
props: { className: 'title', children: 'Hello' },
key: null,
ref: null,
};The real object has a few more internal fields, but type, props, and key are the ones that matter for this tutorial. Creating elements is cheap. They are short-lived and React throws them away after each render.
Host elements and component elements
The type field takes one of two kinds of value:
| Kind | `type` value | Example JSX | Meaning |
|---|---|---|---|
| Host element | A string | `<button>` | A real DOM element to create |
| Component element | A function (or class) | `<Counter />` | A component React must call to get more elements |
JSX uses capitalization to tell them apart. <button> becomes the string 'button'. <Counter /> becomes a reference to the Counter function.
Writing <Counter /> does not call Counter. It creates an element whose type is Counter. React calls the function later, during rendering.
Building a tree
A component's output can contain other component elements, so a render produces a tree of elements. React expands it by calling each component function until only host elements remain.
Here is the matching code:
import { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
return (
<button onClick={() => setCount(count + 1)}>
Clicked {count} times
</button>
);
}
export default function App() {
return (
<main>
<h1>Demo</h1>
<Counter />
</main>
);
}App returns an element tree with main at the root. One child of main is a Counter element. React calls Counter, which returns a button element. At that point the tree has no unexpanded component elements left.
What a component can return
A component can return:
- A single element
- An array or fragment of elements (
<>...</>) - A string or number, which becomes text
null,undefined, or a boolean, which renders nothing
A component must return something other than a bare object that is not an element. Returning a plain object such as { a: 1 } throws an error.
Common errors
- Calling a component like a function. Writing
{Counter()}inside JSX runsCounteras part of the parent's render. Its hooks then attach to the parent, and React sees noCounterelement. Write<Counter />instead. - Lowercase component names.
<counter />becomes the string'counter', so React creates a DOM element with that tag name, not your component. - Defining a component inside another component. Each parent render creates a new function, so React sees a different
typeevery time. Chapter 7 covers why this resets state. - Returning
undefinedby accident. A missingreturnrenders nothing, with no warning in many setups. Check for a stray newline afterreturn.
Why this matters for the rest of the tutorial
React's rendering model rests on one idea. Rendering produces a new tree of element objects, and React compares it with the previous tree to decide what to change in the DOM. The next chapters cover when a render happens and how that comparison works.
Exercise
In <Profile user={u} />, what does React create at the moment the JSX runs, and when does the Profile function actually get called?
Show answer
The JSX creates an element object with type: Profile and props: { user: u }. No DOM node is created and Profile is not called yet. React calls Profile later, during the render phase, to get the elements it returns.
Exercise
A teammate writes {Sidebar()} instead of <Sidebar /> and Sidebar uses useState. Why is this a bug?
Show answer
Calling Sidebar() directly runs it as part of the parent's render, so the parent owns its hooks, and no Sidebar element exists for React to track. Hook order can break when the call is conditional, and Sidebar cannot keep its own state. Use <Sidebar />.