Introducing JSX

Consider this variable declaration:

const element = <h1>Hello, world! </h1>

This funny tag syntax is neither a string nor HTML.

It is called JSX, and it is a syntax extension to JavaScript. We recommend using it with React to describe what the UI should look like. JSX may remind you of a template language, but it comes with the full power of JavaScript.

JSX produces React “elements”. We will explore rendering them to the DOM in the next section. Below, you can find the basics of JSX necessary to get you started.

Rendering Elements

Elements are the smallest building blocks of React apps.

An element describes what you want to see on the screen:

const element = <h1>Hello, world! </h1>

Unlike browser DOM elements, React elements are plain objects, and are cheap to create. React DOM takes care of updating the DOM to match the React elements.

Components and Props

Components let you split the UI into independent, reusable pieces, and think about each piece in isolation.

Conceptually, components are like JavaScript functions. They accept arbitrary inputs (called “props”) and return React elements describing what should appear on the screen.

You can also use an ES6 class to define a component:

class Welcome extends React.Component {
render() {
return <h1>Hello, {this.props.name}</h1>
}
}
State and Lifecycle

We call ReactDOM.render() to change the rendered output:

ReactDOM.render(
<Clock />,
document.getElementById('root')
);

Converting a Function to a Class

Handling Events

Handling events with React elements is very similar to handling events on DOM elements. There are some syntactic differences:

<button onClick={activateLasers}> Activate Lasers <button>