5
Chapter 5
Building UI with Components
React core concepts
There are three core concepts of React that you'll need to be familiar with to start building React applications. These are:
- Components
- Props
- State
In the next chapters, we will go through these concepts and provide resources where you can continue learning them. After you're familiar with these concepts, we'll then show you how to install Next.js and use newer React features such as Server and Client Components.
Components
User interfaces can be broken down into smaller building blocks called components.
Components allow you to build self-contained, reusable snippets of code. If you think of components as LEGO bricks, you can take these individual bricks and combine them together to form larger structures. If you need to update a piece of the UI, you can update the specific component or brick.
This modularity allows your code to be more maintainable as it grows because you can add, update, and delete components without touching the rest of our application.
The nice thing about React components is that they are just JavaScript. Let's see how you can write a React component, from a JavaScript perspective:
Creating components
In React, components are functions. Inside your script
tag, create a new function called header
:
<script type="text/jsx">
const app = document.getElementById("app")
function header() {
}
const root = ReactDOM.createRoot(app);
root.render(<h1>Develop. Preview. Ship.</h1>);
</script>
A component is a function that returns UI elements. Inside the return statement of the function, you can write JSX:
<script type="text/jsx">
const app = document.getElementById("app")
function header() {
return (<h1>Develop. Preview. Ship.</h1>)
}
const root = ReactDOM.createRoot(app);
root.render(<h1>Develop. Preview. Ship.</h1>);
</script>
To render this component to the DOM, pass it as the first argument in the root.render()
method:
<script type="text/jsx">
const app = document.getElementById("app")
function header() {
return (<h1>Develop. Preview. Ship.</h1>)
}
const root = ReactDOM.createRoot(app);
root.render(header);
</script>
But, wait a second. If you try to run the code above in your browser, you'll get an error. To get this to work, there are two things you have to do:
First, React components should be capitalized to distinguish them from plain HTML and JavaScript:
function Header() {
return <h1>Develop. Preview. Ship.</h1>;
}
const root = ReactDOM.createRoot(app);
// Capitalize the React Component
root.render(Header);
Second, you use React components the same way you'd use regular HTML tags, with angle brackets <>
:
function Header() {
return <h1>Develop. Preview. Ship.</h1>;
}
const root = ReactDOM.createRoot(app);
root.render(<Header />);
If you try to run the code in your browser again, you'll see your changes.
Nesting components
Applications usually include more content than a single component. You can nest React components inside each other like you would regular HTML elements.
In your example, create a new component called HomePage
:
function Header() {
return <h1>Develop. Preview. Ship.</h1>;
}
function HomePage() {
return <div></div>;
}
const root = ReactDOM.createRoot(app);
root.render(<Header />);
Then nest the <Header>
component inside the new <HomePage>
component:
function Header() {
return <h1>Develop. Preview. Ship.</h1>;
}
function HomePage() {
return (
<div>
{/* Nesting the Header component */}
<Header />
</div>
);
}
const root = ReactDOM.createRoot(app);
root.render(<Header />);
Component trees
You can keep nesting React components this way to form component trees.
For example, your top-level HomePage
component could hold a Header
, an Article
, and a Footer
Component. And each of those components could in turn have their own child components and so on. For example, the Header
component could contain a Logo
, Title
and Navigation
component.
This modular format allows you to reuse components in different places inside your app.
In your project, since <HomePage>
is now your top-level component, you can pass it to the root.render()
method:
function Header() {
return <h1>Develop. Preview. Ship.</h1>;
}
function HomePage() {
return (
<div>
<Header />
</div>
);
}
const root = ReactDOM.createRoot(app);
root.render(<HomePage />);
Additional Resources:
You've Completed Chapter 5
You've created your first React Components.
Was this helpful?