Sub topic 1
Recap: elements, components and the tree React renders
React Server Components change where parts of a React tree are rendered. To follow that, you first need a precise picture of what React builds when it renders. This chapter restates that picture, because every later chapter builds on it.
Prerequisites
This chapter assumes you already know:
- How to write function components, props and JSX.
- Basic hooks such as
useStateanduseEffect. - Promises and
async/await. - The general difference between client-side and server-side rendering.
Elements are plain objects
A React element is a plain JavaScript object that describes what should appear on screen. JSX is syntax for creating these objects. Creating an element does not render anything and does not call your component.
function Greeting({ name }) {
return <p>Hello, {name}</p>;
}
const element = <Greeting name="Ada" />;
console.log(element.type === Greeting); // true
console.log(element.props); // { name: 'Ada' }The object has a type, a props object and a few bookkeeping fields such as key. The exact bookkeeping fields differ between React versions. The type can be a string such as "p" for a built-in browser element, or a function such as Greeting for a component.
Components are functions that return elements
A component is a function that takes props and returns elements. React calls it when it needs to know what to show. The function runs, returns an element tree, and React repeats the process for any component types it finds in that result.
Note the two roles:
- Your code describes the UI by returning elements.
- React decides when to call your functions and what to do with the result.
Rendering walks the tree
Rendering means React calls components, starting from the root, until only built-in element types remain. The output is a tree. Each node is either a built-in element, such as div, or text.
function Page() {
return (
<main>
<h1>Posts</h1>
<PostList />
</main>
);
}
function PostList() {
return (
<ul>
<li>First post</li>
</ul>
);
}React calls Page, finds PostList in the result, and calls it too.
What a component can hold
Some components keep state with useState, run effects with useEffect, or attach event handlers like onClick. All of these need a running React instance in the browser, because state lives between renders and effects run after the screen updates.
Other components only compute output from props and data. They have no state, no effects and no handlers. This second group matters for what follows, because a component that never needs to run in the browser does not have to be sent to it.
Why this matters for Server Components
The tree is the unit React works with, and an element is only a description of that tree. Descriptions are data, so they can be turned into a serialized form and sent over a network. The next chapters build on that idea.
Keep these three facts in mind:
- An element is an object with a
typeandprops. - A component is a function that React calls to produce elements.
- The rendered result is a tree, and which side calls each function is a choice, not a given.
Exercise
In the Page and PostList example, when does React call PostList, and what does it get back?
Show answer
React calls PostList during rendering, after calling Page and finding a PostList element in the result. It gets back a ul element containing a li element, which are built-in elements, so rendering of that branch is done.