Introduction to JSX and Components
This video starts by setting up a react app using Vite. Ignore this part and just start watching from the JSX chapter. There's also a section on CSS that you can ignore.
function App() {
return (
<h1>Hello</h1>
)
}
export default App
function App() {
return (
<h1>Hello</h1>
)
}
export default App
function App() {
return (
<h1>Hello</h1>
)
}
export default App
function App() {
return (
<h1>Hello</h1>
)
}
export default App
This is similar to a page in Next.js, so follow along with my examples but use the page.tsx
component.
JSX and TSX
JSX is a syntax extension for JavaScript, which means that it gives us new ways to write code that are not part of the JavaScript language. It's like adding new vocabulary or rules to an existing language, so we can express more complex ideas more easily.
function SomeComponent() {
console.log("Some Component")
return (
<div>
<h1 className="text-bold">Hello</h1>
<h2>World</h2>
</div>
)
}
function SomeComponent() {
console.log("Some Component")
return (
<div>
<h1 className="text-bold">Hello</h1>
<h2>World</h2>
</div>
)
}
function SomeComponent() {
console.log("Some Component")
return (
<div>
<h1 className="text-bold">Hello</h1>
<h2>World</h2>
</div>
)
}
function SomeComponent() {
console.log("Some Component")
return (
<div>
<h1 className="text-bold">Hello</h1>
<h2>World</h2>
</div>
)
}
import { jsx } from "react/jsx-runtime";
jsx("div", null,
jsx("h1", { className: "text-bold" }, "Hello"),
jsx("h2", null, "World")
)
import { jsx } from "react/jsx-runtime";
jsx("div", null,
jsx("h1", { className: "text-bold" }, "Hello"),
jsx("h2", null, "World")
)
import { jsx } from "react/jsx-runtime";
jsx("div", null,
jsx("h1", { className: "text-bold" }, "Hello"),
jsx("h2", null, "World")
)
import { jsx } from "react/jsx-runtime";
jsx("div", null,
jsx("h1", { className: "text-bold" }, "Hello"),
jsx("h2", null, "World")
)
Which is just normal JavaScript code that we can run in node, or the browser, or anywhere we can install and run the react library.
{
type: 'div',
props: {
children: [
{
type: 'h1',
props: {
className: 'text-bold',
children: 'Hello'
}
},
{
type: 'h2',
props: {
children: 'World'
}
}
]
}
}
{
type: 'div',
props: {
children: [
{
type: 'h1',
props: {
className: 'text-bold',
children: 'Hello'
}
},
{
type: 'h2',
props: {
children: 'World'
}
}
]
}
}
{
type: 'div',
props: {
children: [
{
type: 'h1',
props: {
className: 'text-bold',
children: 'Hello'
}
},
{
type: 'h2',
props: {
children: 'World'
}
}
]
}
}
{
type: 'div',
props: {
children: [
{
type: 'h1',
props: {
className: 'text-bold',
children: 'Hello'
}
},
{
type: 'h2',
props: {
children: 'World'
}
}
]
}
}
<div>
<h1 class="text-bold">Hello</h1>
<h2>World</h2>
</div>
<div>
<h1 class="text-bold">Hello</h1>
<h2>World</h2>
</div>
<div>
<h1 class="text-bold">Hello</h1>
<h2>World</h2>
</div>
<div>
<h1 class="text-bold">Hello</h1>
<h2>World</h2>
</div>
It seems like JSX is just a way to write HTML looking code in our JavaScript files, but it's actually a way to write JavaScript that eventually gets translated into HTML.
The point of all this is so that we can stay in JavaScript and do everything in JavaScript and never actually touch an HTML file. However, the way we describe a web page is with HTML. Everything we want the user to see eventually gets translated into HTML and CSS.
JavaScript in JSX
So like I said, this is js, so you can inject js into jsx.
<p>6 + 9</p>
<p>6 + 9</p>
<p>6 + 9</p>
<p>6 + 9</p>
<p>{6 + 9}</p>
<p>{6 + 9}</p>
<p>{6 + 9}</p>
<p>{6 + 9}</p>
return (
<div className="App">
<h1 className="heading">Hello</h1>
<h2>World</h2>
<h3>{new Date().toString()}</h3>
</div>
)
return (
<div className="App">
<h1 className="heading">Hello</h1>
<h2>World</h2>
<h3>{new Date().toString()}</h3>
</div>
)
return (
<div className="App">
<h1 className="heading">Hello</h1>
<h2>World</h2>
<h3>{new Date().toString()}</h3>
</div>
)
return (
<div className="App">
<h1 className="heading">Hello</h1>
<h2>World</h2>
<h3>{new Date().toString()}</h3>
</div>
)
A component couples the UI logic and markup together in the same file. So instead of separating your js and html into separate files, it all goes in the same file and we split the app into separate components instead.
Rendering logic and markup live together in the same place—components.
More components and Child components
function SomeNewComponent() {
return (
<h1>Some Markup</h1>
)
}
function SomeNewComponent() {
return (
<h1>Some Markup</h1>
)
}
function SomeNewComponent() {
return (
<h1>Some Markup</h1>
)
}
function SomeNewComponent() {
return (
<h1>Some Markup</h1>
)
}
Component Files
It's common to put each component in their own file, even if they're very small like this one.
some-new-component.jsx
some-new-component.jsx
some-new-component.jsx
some-new-component.jsx
export default function SomeNewComponent() {
return (
<h1>Some Markup</h1>
)
}
export default function SomeNewComponent() {
return (
<h1>Some Markup</h1>
)
}
export default function SomeNewComponent() {
return (
<h1>Some Markup</h1>
)
}
export default function SomeNewComponent() {
return (
<h1>Some Markup</h1>
)
}
import SomeNewComponent from './some-new-component'
import SomeNewComponent from './some-new-component'
import SomeNewComponent from './some-new-component'
import SomeNewComponent from './some-new-component'
function Page() {
return (
<div className="App">
<h1 className="heading">Hello</h1>
<h2>World</h2>
<h3>{new Date().toString()}</h3>
<SomeNewComponent />
</div>
)
}
function Page() {
return (
<div className="App">
<h1 className="heading">Hello</h1>
<h2>World</h2>
<h3>{new Date().toString()}</h3>
<SomeNewComponent />
</div>
)
}
function Page() {
return (
<div className="App">
<h1 className="heading">Hello</h1>
<h2>World</h2>
<h3>{new Date().toString()}</h3>
<SomeNewComponent />
</div>
)
}
function Page() {
return (
<div className="App">
<h1 className="heading">Hello</h1>
<h2>World</h2>
<h3>{new Date().toString()}</h3>
<SomeNewComponent />
</div>
)
}
The Rules of JSX and TSX
return (
<div>
<h1 className="header">Hello</h1>
<h2>World</h2>
<img className="image" src="https://picsum.photos/200" alt="random"></img>
</div>
)
return (
<div>
<h1 className="header">Hello</h1>
<h2>World</h2>
<img className="image" src="https://picsum.photos/200" alt="random"></img>
</div>
)
return (
<div>
<h1 className="header">Hello</h1>
<h2>World</h2>
<img className="image" src="https://picsum.photos/200" alt="random"></img>
</div>
)
return (
<div>
<h1 className="header">Hello</h1>
<h2>World</h2>
<img className="image" src="https://picsum.photos/200" alt="random"></img>
</div>
)
return (
<div>
<h1 className="header">Hello</h1>
<h2>World</h2>
<img className="image" src="https://picsum.photos/200" alt="random"></img>
</div>
)
return (
<div>
<h1 className="header">Hello</h1>
<h2>World</h2>
<img className="image" src="https://picsum.photos/200" alt="random"></img>
</div>
)
return (
<div>
<h1 className="header">Hello</h1>
<h2>World</h2>
<img className="image" src="https://picsum.photos/200" alt="random"></img>
</div>
)
return (
<div>
<h1 className="header">Hello</h1>
<h2>World</h2>
<img className="image" src="https://picsum.photos/200" alt="random"></img>
</div>
)
return (
<div>
<h1 className="header">Hello</h1>
<h2>World</h2>
<img className="image" src="https://picsum.photos/200" alt="random"></img>
</div>
)
return (
<div>
<h1 className="header">Hello</h1>
<h2>World</h2>
<img className="image" src="https://picsum.photos/200" alt="random"></img>
</div>
)
return (
<div>
<h1 className="header">Hello</h1>
<h2>World</h2>
<img className="image" src="https://picsum.photos/200" alt="random"></img>
</div>
)
return (
<div>
<h1 className="header">Hello</h1>
<h2>World</h2>
<img className="image" src="https://picsum.photos/200" alt="random"></img>
</div>
)