Thinking in Components
💻 ReactUnderstand components, JSX and how React renders a page.
0 of 9 completed
React breaks an interface into small components. Each component is a function that returns markup, and React keeps the page in sync with the data.
By the end of this lesson you can:
- Write a component.
- Use JSX expressions.
- Render a component at the root.
A Component
A component is a function with a capitalised name. It returns JSX, which looks like HTML but is JavaScript.
function Welcome() {
const name = "Amina";
return (
<section>
<h1>Welcome, {name}</h1>
<p>Today is {new Date().toLocaleDateString()}.</p>
</section>
);
}
export default Welcome;JSX Rules
- Return one parent element.
- Close every tag, including
<img />. - Use
classNameinstead ofclass. - Embed JavaScript in curly braces.
Render at the Root
Create a root and render your top-level component into it.
import { createRoot } from "react-dom/client";
import Welcome from "./Welcome";
createRoot(document.getElementById("root")).render(<Welcome />);Tip Start with a static component. Add state only when something must change in response to the user.
Practice
- Build a component that shows your name and school.
- Embed a calculation inside curly braces.
- Explain why a component name must start with a capital letter.
Hands-on exercise
Create a ProfileCard component that shows:
1. A heading with a name.
2. A paragraph with a role.
3. A list of three skills.
Recap
Components are functions that return JSX. Keep them small and render one root component to start.
Example
function Welcome() {
const name = "Amina";
return (
<section>
<h1>Welcome, {name}</h1>
<p>Today is {new Date().toLocaleDateString()}.</p>
</section>
);
}
export default Welcome;
Exercises
Exercise 1. Profile card
Easy
Create a ProfileCard component that shows:
1. A heading with a name.
2. A paragraph with a role.
3. A list of three skills.
1. A heading with a name.
2. A paragraph with a role.
3. A list of three skills.
React
exercise-1.txt
Output
Expected Output
A card with a name, role and three skills.
Lesson Nav
Course contents
React Course
0/9 lessons completed
- 1 Thinking in Components Tutorial
- 2 Props and Composition Tutorial
- 3 State and Events Tutorial
- 4 Lists, Keys and Conditional Rendering Tutorial
- 5 Forms in React Tutorial
- 6 Effects and Data Fetching Tutorial
- 7 Routing and Layouts Tutorial
- 8 Context and Shared State Tutorial
- 9 Project: A Student Dashboard Project
Exercises
Exercise 1 Profile card