React · Lesson 4 of 120
JSX
HTML in JavaScript.
The HTML-like code inside a component is called JSX. It looks like HTML, but it's really JavaScript. A build tool turns each tag into a function call that React understands.
- Looks like HTML
- Really JavaScript
- Turned into calls
A JSX page
This component returns a heading, a short paragraph, and a list of three stores. It reads just like HTML. Run it.
App.jsxjsx
export default function App() {
return (
<div>
<h1>Our stores</h1>
<p>Visit us in Yorkshire:</p>
<ul>
<li>Leeds</li>
<li>York</li>
<li>Hull</li>
</ul>
</div>
);
}Also uses style.css
body{font-family:-apple-system,Helvetica,Arial,sans-serif;margin:22px;font-size:21px;color:#1d1d1f;background:#fafafa}
h1{font-size:34px;margin:0 0 14px} h2{font-size:26px;margin:0 0 10px}
button{font-size:19px;padding:9px 18px;border-radius:9px;border:0;background:#61dafb;color:#111;font-weight:600;margin:4px 6px 4px 0}
input{font-size:19px;padding:8px 10px;border:2px solid #bbb;border-radius:8px}
li{margin:5px 0} .box{padding:14px;border-radius:12px;background:#fff;border:2px solid #ddd;margin:8px 0}
.done{text-decoration:line-through;color:#999} small{color:crimson;display:block;font-size:16px} .big{font-size:64px;font-weight:800}Why JSX
The markup and the logic for one piece of the page live together, in one component. That makes components easy to read, and easy to move around. And because it's JavaScript, mistakes show up straight away.
| key | value |
|---|---|
| Markup + logic | together |
| Components | easy to move |
| Mistakes | found early |
| Compiled to | React calls |
Summary
JSX looks like HTML, but compiles to JavaScript. Next, its few rules.
Key takeaways- JSX
- HTML-like
- Compiled
Next up: JSX Rules — one parent, close every tag