LEARN REACT
Quick Start
Welcome to the React documentation! This page will give you an
introduction to 80% of the React concepts that you will use on a daily
basis.
You will learn
How to create and nest components
How to add markup and styles
How to display data
How to render conditions and lists
How to respond to events and update the screen
How to share data between components
Creating and nesting components
React apps are made out of components. A component is a piece of the UI (user
interface) that has its own logic and appearance. A component can be as small
as a button, or as large as an entire page.
React components are JavaScript functions that return markup:
function MyButton() {
return (
<button> I'm a button </button>
);
}
Now that you’ve declared MyButton, you can nest it into another component:
export default function MyApp() {
return (
<div>
<h1> Welcome to my app </h1>
<MyButton />
</div>
);
}
Notice that <MyButton /> starts with a capital letter. That’s how you know it’s a
React component. React component names must always start with a capital
letter, while HTML tags must be lowercase.
Have a look at the result:
App.js
DownloadReset
Fork
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function MyButton() {
return (
<button>
I'm a button
</button>
);
}
export default function MyApp() {
return (
<div>
<h1>Welcome to my app</h1>
<MyButton />
</div>
);
}
Show more
The export default keywords specify the main component in the file. If you’re not
familiar with some piece of JavaScript syntax, MDN and javascript.info have
great references.
Writing markup with JSX
The markup syntax you’ve seen above is called JSX. It is optional, but most
React projects use JSX for its convenience. All of the tools we recommend for
local development support JSX out of the box.
JSX is stricter than HTML. You have to close tags like <br />. Your component
also can’t return multiple JSX tags. You have to wrap them into a shared parent,
like a <div>...</div> or an empty <>...</> wrapper:
function AboutPage() {
return (
<>
<h1> About </h1>
<p> Hello there. <br /> How do you do? </p>
</>
);
}
If you have a lot of HTML to port to JSX, you can use an online converter.
Adding styles
In React, you specify a CSS class with className. It works the same way as the
HTML class attribute:
<img className = "avatar" />
Then you write the CSS rules for it in a separate CSS file:
/* In your CSS */
.avatar {
border-radius: 50%;
}
React does not prescribe how you add CSS files. In the simplest case, you’ll add
a <link> tag to your HTML. If you use a build tool or a framework, consult its
documentation to learn how to add a CSS file to your project.
Displaying data
JSX lets you put markup into JavaScript. Curly braces let you “escape back” into
JavaScript so that you can embed some variable from your code and display it to
the user. For example, this will display user.name:
return (
<h1>
{user . name}
</h1>
);