Lesson 2 of 11
Lesson Progress: 0%
JSX is a syntax style used in React that lets you write code that looks a lot like HTML inside JavaScript. It was created to make building user interfaces easier to read and easier to think about, especially when a page has many parts. Before JSX, React code often used many React.createElement(...) calls, which worked fine but could become hard to read as the page grew. JSX gave developers a cleaner way to describe what they wanted to appear on the screen. Even though it looks like HTML, JSX is not exactly HTML; it gets changed into regular JavaScript before the browser uses it. One big advantage of JSX is that it makes component code shorter, clearer, and easier to organize. This helps people focus on the structure of the page instead of getting lost in long nested function calls. JSX also makes it easier to update the DOM because you describe the UI in a simple way, and React handles the work of figuring out what needs to change in the real page. In that way, JSX helps developers control and change the DOM more easily by letting them write a clear picture of the page while React manages the updates behind the scenes.
React.createElement(...) lines.Instructions
▼ ← Click the triangle to hide or reveal instructions.h1 heading.return <h1>Hello, World!</h1>; gives that heading back to React.Instructions
▼ ← Click the triangle to hide or reveal instructions.div with an h1 inside it.return part gives that HTML back to React so it can be shown.div wraps around the h1.Instructions
▼ ← Click the triangle to hide or reveal instructions.div wraps around everything inside.div, h1, h2, and p.Instructions
▼ ← Click the triangle to hide or reveal instructions.ul tag wraps everything, so it follows the one outer tag rule.<SimpleList /> means use the SimpleList component here.render(<SimpleList />) is the usual JSX way to show the component on the page.render(SimpleList) is more like a simple shortcut idea, but render(<SimpleList />) is the more common real React form.style={{ ... }} part uses an object to hold the CSS settings.listStyleType: "disc" makes the list use round bullet points.paddingLeft: "20px" moves the list a little to the right.Instructions
▼ ← Click the triangle to hide or reveal instructions.ul is the one outer tag around the list.style={{ listStyleType: "disc", paddingLeft: "20px" }} part helps show the list dots and adds space on the left{animal1} part places the value of animal1 into the first list item.{animal2} part places the value of animal2 into the second list item.render(<AnotherList />) is the usual JSX way to show the component, while render(AnotherList) is more like a simple shortcut idea.Instructions
▼ ← Click the triangle to hide or reveal instructions.table, which wraps around all the rows and cells.style={{ border: "1px solid black" }} part adds a black border around each table cell.render(<SimpleTable />) is the usual JSX way to show the component, while render(SimpleTable) is more like a simple shortcut idea.Instructions
▼ ← Click the triangle to hide or reveal instructions.div.background-color becomes backgroundColor in JSX.Instructions
▼ ← Click the triangle to hide or reveal instructions.div.background-color becomes backgroundColor in JSX.border-radius becomes borderRadius in JSX.Test Incomplete
function HelloMessage() {
return <h1>Hello, World!</h1>;
}
render(HelloMessage);function ReactRocks() {
const message = "React rocks!";
return (
<div>
<h1>React rocks!</h1>
</div>
);
}
render(ReactRocks);function WelcomeMessage() {
return (
<div>
<h2>Welcome</h2>
<p>This is a basic React component.</p>
</div>
);
}
render(WelcomeMessage);function SimpleList() {
return (
<ul style={{ listStyleType: "disc", paddingLeft: "20px" }}>
<li>Apple</li>
<li>Banana</li>
<li>Orange</li>
</ul>
);
}
render(<SimpleList/>);function AnotherList() {
const animal1 = "dog";
const animal2 = "bird";
const animal3 = "cat";
return (
<ul style={{ listStyleType: "disc", paddingLeft: "20px" }}>
<li>{animal1}</li>
<li>{animal2}</li>
<li>{animal3}</li>
</ul>
);
}
render(<AnotherList/>);function SimpleTable() {
return (
<table border="1">
<tbody>
<tr>
<td style={{ border: "1px solid black" }}>Apple</td>
<td style={{ border: "1px solid black" }}>Red</td>
</tr>
<tr>
<td style={{ border: "1px solid black" }}>Banana</td>
<td style={{ border: "1px solid black" }}>Yellow</td>
</tr>
</tbody>
</table>
);
}
render(<SimpleTable/>);function PurpleSquare() {
return (
<div
style={{
width: "100px",
height: "100px",
backgroundColor: "purple"
}}
></div>
);
}
render(<PurpleSquare />);function BlueCircle() {
return (
<div
style={{
width: "100px",
height: "100px",
backgroundColor: "blue",
borderRadius: "50%"
}}
></div>
);
}
render(<BlueCircle/>);