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() {
function App() {
function App() {
function 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() {
function SomeComponent() {
function SomeComponent() {
function SomeComponent() {
import { jsx } from "react/jsx-runtime";
import { jsx } from "react/jsx-runtime";
import { jsx } from "react/jsx-runtime";
import { jsx } from "react/jsx-runtime";
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.
{
{
{
{
<div>
<div>
<div>
<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 (
return (
return (
return (
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() {
function SomeNewComponent() {
function SomeNewComponent() {
function SomeNewComponent() {
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() {
export default function SomeNewComponent() {
export default function SomeNewComponent() {
export default function SomeNewComponent() {
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() {
function Page() {
function Page() {
function Page() {
The Rules of JSX and TSX
return (
return (
return (
return (
return (
return (
return (
return (
return (
return (
return (
return (