--- title: 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. - 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 {/*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: ```js function MyButton() { return ( ); } ``` Now that you've declared `MyButton`, you can nest it into another component: ```js {5} export default function MyApp() { return (

Welcome to my app

); } ``` Notice that `` 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: ```js function MyButton() { return ( ); } export default function MyApp() { return (

Welcome to my app

); } ```
The `export default` keywords specify the main component in the file. If you're not familiar with some piece of JavaScript syntax, [MDN](https://developer.mozilla.org/en-US/docs/web/javascript/reference/statements/export) and [javascript.info](https://javascript.info/import-export) have great references. ## Writing markup with JSX {/*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](/learn/installation) support JSX out of the box. JSX is stricter than HTML. You have to close tags like `
`. Your component also can't return multiple JSX tags. You have to wrap them into a shared parent, like a `
...
` or an empty `<>...` wrapper: ```js {3,6} function AboutPage() { return ( <>

About

Hello there.
How do you do?

); } ``` If you have a lot of HTML to port to JSX, you can use an [online converter.](https://transform.tools/html-to-jsx) ## Adding styles {/*adding-styles*/} In React, you specify a CSS class with `className`. It works the same way as HTML [`class`](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/class) attribute: ```js ``` Then you write the CSS rules for it in a separate CSS file: ```css /* In your CSS */ .avatar { border-radius: 50%; } ``` React does not prescribe how you add CSS files. In the simplest case, you'll add a [``](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/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 {/*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`: ```js {3} return (

{user.name}

); ``` 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: ```js {3,4} return ( ); ``` You can put more complex expressions inside the JSX curly braces too, for example, [string concatenation](https://javascript.info/operators#string-concatenation-with-binary): ```js const user = { name: 'Hedy Lamarr', imageUrl: 'https://i.imgur.com/yXOvdOSs.jpg', imageSize: 90, }; export default function Profile() { return ( <>

{user.name}

{'Photo ); } ``` ```css .avatar { border-radius: 50%; } .large { border: 4px solid gold; } ```
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 {/*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`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else) statement to conditionally include JSX: ```js let content; if (isLoggedIn) { content = ; } else { content = ; } return (
{content}
); ``` If you prefer more compact code, you can use the [conditional `?` operator.](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator) Unlike `if`, it works inside JSX: ```js
{isLoggedIn ? ( ) : ( )}
``` When you don't need the `else` branch, you can also use a shorter [logical `&&` syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_AND#short-circuit_evaluation): ```js
{isLoggedIn && }
``` 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 {/*rendering-lists*/} You will rely on JavaScript features like [`for` loop](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for) and the [array `map()` function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) to render lists of components. For example, let's say you have an array of products: ```js 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 `
  • ` items: ```js const listItems = products.map(product =>
  • {product.title}
  • ); return (
      {listItems}
    ); ``` Notice how `
  • ` 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 will rely on your keys to understand what happened if you later insert, delete, or reorder the items. ```js const products = [ { title: 'Cabbage', isFruit: false, id: 1 }, { title: 'Garlic', isFruit: false, id: 2 }, { title: 'Apple', isFruit: true, id: 3 }, ]; export default function ShoppingList() { const listItems = products.map(product =>
  • {product.title}
  • ); return (
      {listItems}
    ); } ``` ## Responding to events {/*responding-to-events*/} You can respond to events by declaring *event handler* functions inside your components: ```js {2-4,7} function MyButton() { function handleClick() { alert('You clicked me!'); } return ( ); } ``` 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. ## Updating the screen {/*updating-the-screen*/} Often, you'll want your component to "remember" some information and display it. For example, maybe you want to count the number of times a button is clicked. To do this, add *state* to your component. First, import [`useState`](/apis/react/useState) from React: ```js import { useState } from 'react'; ``` Now you can declare a *state variable* inside your component: ```js function MyButton() { const [count, setCount] = useState(0); ``` You will 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 call them like `[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 to it. Clicking this button will increment the counter: ```js {5} function MyButton() { const [count, setCount] = useState(0); function handleClick() { setCount(count + 1); } return ( ); } ``` 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. Try clicking each button separately: ```js import { useState } from 'react'; function MyButton() { const [count, setCount] = useState(0); function handleClick() { setCount(count + 1); } return ( ); } export default function MyApp() { return (

    Counters that update separately

    ); } ``` ```css button { display: block; margin-bottom: 5px; } ```
    Notice how each button "remembers" its own `count` state and doesn't affect other buttons. ## Using Hooks {/*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 [React API reference.](/apis/react) You can also write your own Hooks by combining the existing ones. Hooks are more restrictive than regular functions. You can only call Hooks *at the top level* 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. ## Sharing data between components {/*sharing-data-between-components*/} In the previous example, each `MyButton` had its own independent `count`, and when each button was clicked, only the `count` for the button clicked changed: Initially, each `MyButton`'s `count` state is `0` The first `MyButton` updates its `count` to `1` However, often you'll need components to *share data and always update together*. 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. In this example, it is `MyApp`: Initially, `MyApp`'s `count` state is `0` and is passed down to both children On click, `MyApp` updates its `count` state to `1` and passes it down to both children 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. First, *move the state up* from `MyButton` into `MyApp`: ```js {2,6-10} function MyButton() { // ... we're moving code from here ... } export default function MyApp() { const [count, setCount] = useState(0); function handleClick() { setCount(count + 1); } return (

    Counters that update separately

    ); } ``` 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 ``: ```js {11-12} export default function MyApp() { const [count, setCount] = useState(0); function handleClick() { setCount(count + 1); } return (

    Counters that update together

    ); } ``` 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: ```js {1,3} function MyButton({ count, onClick }) { return ( ); } ``` 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, we've shared it between components. ```js import { useState } from 'react'; function MyButton({ count, onClick }) { return ( ); } export default function MyApp() { const [count, setCount] = useState(0); function handleClick() { setCount(count + 1); } return (

    Counters that update together

    ); } ``` ```css button { display: block; margin-bottom: 5px; } ```
    ## Next Steps {/*next-steps*/} By now, you know the basics of how to write React code! Head to [Thinking in React](/learn/thinking-in-react) to see how it feels to build a UI with React in practice.