React Tutorial
React Tutorial
LEARN REACT
Quick Start
Welcome to the React documentation! This page will give you an
introduction to the 80% of React concepts that you will use on a daily
basis.
function MyButton() {
return (
<button>I'm a button</button>
);
https://react.dev/learn 1/19
4/28/23, 11:46 AM Quick Start – React
Now that you’ve declared MyButton , you can nest it into another component:
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.
function MyButton() {
return (
<button>
I'm a button
</button>
);
}
Show more
https://react.dev/learn 2/19
4/28/23, 11:46 AM Quick Start – React
Welcome to my app
I'm a button
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.
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>
https://react.dev/learn 3/19
4/28/23, 11:46 AM Quick Start – React
</>
);
}
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:
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 :
https://react.dev/learn 4/19
4/28/23, 11:46 AM Quick Start – React
return (
<h1>
{user.name}
</h1>
);
You can also “escape into JavaScript” from JSX attributes, but you have to use curly
braces instead of quotes. For example, className="avatar" passes the "avatar"
string as the CSS class, but src={user.imageUrl} reads the JavaScript
user.imageUrl variable value, and then passes that value as the src attribute:
return (
<img
className="avatar"
src={user.imageUrl}
/>
);
You can put more complex expressions inside the JSX curly braces too, for example,
string concatenation:
const user = {
name: 'Hedy Lamarr',
imageUrl: 'https://i.imgur.com/yXOvdOSs.jpg',
imageSize: 90,
};
https://react.dev/learn 5/19
4/28/23, 11:46 AM Quick Start – React
l " "
Show more
In the above example, style={{}} is not a special syntax, but a regular {} object
inside the style={ } JSX curly braces. You can use the style attribute when your
styles depend on JavaScript variables.
Conditional rendering
In React, there is no special syntax for writing conditions. Instead, you’ll use the same
techniques as you use when writing regular JavaScript code. For example, you can use
an if statement to conditionally include JSX:
let content;
if (isLoggedIn) {
content = <AdminPanel />;
} else {
content = <LoginForm />;
}
return (
https://react.dev/learn 6/19
4/28/23, 11:46 AM Quick Start – React
<div>
{content}
</div>
);
If you prefer more compact code, you can use the conditional ? operator. Unlike if , it
works inside JSX:
<div>
{isLoggedIn ? (
<AdminPanel />
) : (
<LoginForm />
)}
</div>
When you don’t need the else branch, you can also use a shorter logical && syntax:
<div>
{isLoggedIn && <AdminPanel />}
</div>
All of these approaches also work for conditionally specifying attributes. If you’re
unfamiliar with some of this JavaScript syntax, you can start by always using
if...else .
Rendering lists
You will rely on JavaScript features like for loop and the array map() function to
render lists of components.
https://react.dev/learn 7/19
4/28/23, 11:46 AM Quick Start – React
const products = [
{ title: 'Cabbage', id: 1 },
{ title: 'Garlic', id: 2 },
{ title: 'Apple', id: 3 },
];
Inside your component, use the map() function to transform an array of products into
an array of <li> items:
return (
<ul>{listItems}</ul>
);
Notice how <li> has a key attribute. For each item in a list, you should pass a string
or a number that uniquely identifies that item among its siblings. Usually, a key should
be coming from your data, such as a database ID. React uses your keys to know what
happened if you later insert, delete, or reorder the items.
const products = [
{ title: 'Cabbage', isFruit: false, id: 1 },
{ title: 'Garlic', isFruit: false, id: 2 },
{ title: 'Apple', isFruit: true, id: 3 },
];
https://react.dev/learn 8/19
4/28/23, 11:46 AM Quick Start – React
<li
key={product.id}
style={{
l d i i ? ' ' 'd k '
Show more
Responding to events
You can respond to events by declaring event handler functions inside your
components:
function MyButton() {
function handleClick() {
alert('You clicked me!');
}
return (
<button onClick={handleClick}>
Click me
</button>
);
https://react.dev/learn 9/19
4/28/23, 11:46 AM Quick Start – React
Notice how onClick={handleClick} has no parentheses at the end! Do not call the
event handler function: you only need to pass it down. React will call your event
handler when the user clicks the button.
function MyButton() {
const [count, setCount] = useState(0);
// ...
You’ll get two things from useState : the current state ( count ), and the function that
lets you update it ( setCount ). You can give them any names, but the convention is to
write [something, setSomething] .
The first time the button is displayed, count will be 0 because you passed 0 to
useState() . When you want to change state, call setCount() and pass the new value
function MyButton() {
https://react.dev/learn 10/19
4/28/23, 11:46 AM Quick Start – React
function handleClick() {
setCount(count + 1);
}
return (
<button onClick={handleClick}>
Clicked {count} times
</button>
);
}
React will call your component function again. This time, count will be 1 . Then it will
be 2 . And so on.
If you render the same component multiple times, each will get its own state. Click
each button separately:
Show more
https://react.dev/learn 11/19
4/28/23, 11:46 AM Quick Start – React
Notice how each button “remembers” its own count state and doesn’t affect other
buttons.
Using Hooks
Functions starting with use are called Hooks. useState is a built-in Hook provided by
React. You can find other built-in Hooks in the API reference. You can also write your
own Hooks by combining the existing ones.
Hooks are more restrictive than other functions. You can only call Hooks at the top of
your components (or other Hooks). If you want to use useState in a condition or a
loop, extract a new component and put it there.
https://react.dev/learn 12/19
4/28/23, 11:46 AM Quick Start – React
However, often you’ll need components to share data and always update together.
https://react.dev/learn 13/19
4/28/23, 11:46 AM Quick Start – React
To make both MyButton components display the same count and update together,
you need to move the state from the individual buttons “upwards” to the closest
component containing all of them.
https://react.dev/learn 14/19
4/28/23, 11:46 AM Quick Start – React
Now when you click either button, the count in MyApp will change, which will change
both of the counts in MyButton . Here’s how you can express this in code.
function handleClick() {
setCount(count + 1);
}
return (
<div>
<h1>Counters that update separately</h1>
<MyButton />
<MyButton />
</div>
https://react.dev/learn 15/19
4/28/23, 11:46 AM Quick Start – React
);
}
function MyButton() {
// ... we're moving code from here ...
}
Then, pass the state down from MyApp to each MyButton , together with the shared
click handler. You can pass information to MyButton using the JSX curly braces, just
like you previously did with built-in tags like <img> :
function handleClick() {
setCount(count + 1);
}
return (
<div>
<h1>Counters that update together</h1>
<MyButton count={count} onClick={handleClick} />
<MyButton count={count} onClick={handleClick} />
</div>
);
}
The information you pass down like this is called props. Now the MyApp component
contains the count state and the handleClick event handler, and passes both of them
down as props to each of the buttons.
Finally, change MyButton to read the props you have passed from its parent
component:
return (
<button onClick={onClick}>
Clicked {count} times
</button>
);
}
When you click the button, the onClick handler fires. Each button’s onClick prop
was set to the handleClick function inside MyApp , so the code inside of it runs. That
code calls setCount(count + 1) , incrementing the count state variable. The new
count value is passed as a prop to each button, so they all show the new value. This is
called “lifting state up”. By moving state up, you’ve shared it between components.
function handleClick() {
setCount(count + 1);
}
return (
<div>
<h1>Counters that update together</h1>
Show more
https://react.dev/learn 17/19
4/28/23, 11:46 AM Quick Start – React
Next Steps
By now, you know the basics of how to write React code!
Check out the Tutorial to put them into practice and build your first mini-app with
React.
NEXT
Tutorial: Tic-Tac-Toe
©2023
https://react.dev/learn 18/19
4/28/23, 11:46 AM Quick Start – React
Describing the UI
Adding Interactivity
Managing State
Escape Hatches
Community More
Acknowledgements Terms
https://react.dev/learn 19/19