Your First Function Component
What you'll learn: How to write your very first React function component and see it appear on the page.
Writing a Component is Just Writing a Function
A React function component is simply a JavaScript function that returns JSX. That's it. The function name should start with a capital letter (this is how React distinguishes components from regular HTML tags), and whatever JSX you return becomes what appears on the screen.
Here's the simplest possible component:
function Welcome() {
return <h1>Hello, React!</h1>;
}
Notice three key things:
- Function name starts with a capital letter (
Welcome, notwelcome) - Returns JSX (that
<h1>element) - No parameters yet (we'll add those later)
Rendering Your Component to the Page
To make your component appear, you use it like an HTML tag inside another piece of JSX:
function Welcome() {
return <h1>Hello, React!</h1>;
}
// Render it to the page
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Welcome />);
Notice the self-closing tag syntax: <Welcome />. This tells React to call your Welcome function and put its returned JSX on the page.
Think of it like a custom HTML tag you invented. You defined what <Welcome /> means, and React handles calling your function and displaying the result.
Key Takeaway: A function component is a JavaScript function (with a capital first letter) that returns JSX. You render it by using it as a JSX tag, and React calls your function to display what it returns.