React Blog Project
This chapter takes you from scratch to create a React blog project with Vite and understand JSX syntax and project structure.
Environment Preparation
Before starting, make sure Node.js is installed on your computer (v16 or above recommended).
Node.js is the runtime environment for JavaScript, and npm is its built-in package manager.
Check If Node.js Is Installed
$ node -v
v20.10.0
If a version number is displayed, it is installed.
If not, go to nodejs.org Download the LTS version to install.
Creating a React Project
Vite is currently one of the fastest front-end build tools, with native support for React.
In the terminal, navigate to the directory where you want to store the project and run:
$ npm create vite@latest blog-app -- --template react
This command tells Vite: create a project named blog-app using the React template.
Enter the project directory, install dependencies, and start the development server:
$ cd blog-app
$ npm install
$ npm run dev
The browser will automatically open http://localhost:5173; seeing the default Vite + React welcome page means the project started successfully.
If startup reports an error about missing dependencies, you can try running
npm installand then run again.
Detailed Project Directory Structure
Open the blog-app folder and you will see the following structure:
blog-app/
├── index.html # 入口 HTML 文件
├── package.json # 项目配置与依赖列表
├── vite.config.js # Vite 构建工具配置
├── public/ # 不经过编译的静态资源
├── src/
│ ├── main.jsx # JS 入口,将 React 组件挂载到 DOM
│ ├── App.jsx # 根组件,所有页面的外壳
│ ├── App.css # App 组件的样式
│ ├── index.css # 全局样式
│ └── assets/ # 需要编译的静态资源(图片等)
Roles of Core Files
index.html — The entry file accessed by the browser.
Inside, there is a , and the React app mounts onto this div.
src/main.jsx — the startup file of the React app.
It is responsible for finding the #root element and then using the createRoot Puts the component to render into it.
Example
// File path: src/main.jsx
import
React from
'react'
import
ReactDOM from
'react-dom/client'
import
App from
'./App.jsx'
import
'./index.css'
// Find the in HTML
// Use createRoot to create a React root node, then
ReactDOM.
createRoot
(
document.
getElementById
(
'root'
)
)
.
render
(
<
React.
StrictMode
<
App
/>
React.
StrictMode
)
src/App.jsx — the root component.
It is the outermost container for all pages, and the JSX it returns is rendered onto the page.
is React's Strict Mode, which only takes effect in development. It performs extra checks on components (such as repeated rendering) to help you find potential issues. Production builds ignore it automatically.
What Is JSX?
JSX is a syntax extension of JavaScript that lets you write HTML-like code in JS.
A React component is essentially a function that returns JSX.
Key Differences Between JSX and HTML
| Scenario | HTML | JSX |
|---|---|---|
| class attribute | class="container" |
className="container" |
| Self-closing tags | |
|
| Embedding JS expressions | Not supported | |
| style attribute | style="color: red" |
style={{ color: 'red' }} |
| Notes | |
{/* 注释 */} |
| Event binding | onclick="fn()" |
onClick={fn} |
Why use
classNameinstead ofclass? Because JSX is essentially JavaScript, andclassis a reserved keyword in JS. Similarly,fortohtmlFor。
Example
// JSX is this HTML-like code after return
function
Greeting
(
)
{
const
name
=
'RUNOOB'
const
isLoggedIn
=
true
return
(
<
div className
=
"greeting"
{
/* JSX comment: put any JS expression inside curly braces */
}
<h1 Hello,{name}>!
h1
<
p
Current time:
{
new
Date
(
)
.
toLocaleDateString
(
)
}
p
{
isLoggedIn
&&
<
p
Welcome back!
p
}
div
)
}
Remember the core rule: you can put any JS expression inside curly braces in JSX - variables, function calls, ternary operations, array map, all work.
React Component = Function
The essence of a React function component is: a function that takes props as parameters and returns a piece of JSX.
Example
// Define a component: it is a function that returns JSX
// Component names must start with a capital letter
function
BlogHeader
(
)
{
return
(
<
header
<
h1RUNOOB Blog
h1
header
)
}
// Using a component: write it like an HTML tag, self-closing or paired both work
function
App
(
)
{
return
(
<
div
<
BlogHeader
/>
<
main
<
pBlog content will be displayed here
p
main
div
)
}
export
default
App
Component names muststart with a capital letter. Starting with lowercase makes React treat it as a regular HTML tag (div, span, etc.); only starting with uppercase is it treated as a custom component.
Hands-On: Clear the Template and Write the Blog Skeleton
Clear the default generated content of App.jsx and App.css, replacing it with the basic structure of a blog.
Example
// File path: src/App.jsx
import
'./App.css'
function
App
(
)
{
return
(
<
div className
=
"app"
{
/* Top navigation bar */
}
<
header className
=
"navbar"
<
h1 className
=
"logo"
RUNOOB Blog
h1
<
nav
<a href
=
"/"
Home
a
<
a href
=
"#"
About
a
nav
header
{
/* Main content area */
}
<
main className
=
"container"
<
p
Blog content will be displayed here
p
main
{
/* Footer */
}
<
footer className
=
"footer"
<
p
©
2024RUNOOB Blog.
Powered
by React.
p
footer
div
)
}
export
default
App
Example
/* File path: src/App.css */
{
margin
:
0
;
padding
:
0
;
box-sizing
:
border-box
;
}
body
{
font-family
:
-apple-system
,
BlinkMacSystemFont
,
'Segoe UI'
,
Roboto
,
sans-serif
;
background
:
#f5f5f5
;
color
:
#333
;
}
/* Navigation bar */
.navbar
{
display
:
flex
;
justify-content
:
space-between
;
align-items
:
center
;
padding
:
16px
40px
;
background
:
#fff
;
box-shadow
:
0
2px
8px
rgba
(
0
,
0
,
0
,
0.1
)
;
}
.logo
{
font-size
:
24px
;
color
:
#61dafb
;
}
.navbar
nav a
{
margin-left
:
20px
;
text-decoration
:
none
;
color
:
#333
;
}
/* Main content area */
.container
{
max-width
:
960px
;
margin
:
40px
auto
;
padding
:
0
20px
;
}
/* Footer */
.footer
{
text-align
:
center
;
padding
:
20px
;
color
:
#999
;
border-top
:
1px
solid
#eee
;
}
After saving, the browser refreshes automatically, and you will see a clean blog skeleton: top navbar + middle content area + bottom footer.
Chapter Summary
In this chapter you accomplished three things: created a React project with Vite, understood the key differences between JSX and HTML, and wrote the basic JSX skeleton of a blog.
Core takeaway: React components are functions that return JSX, and any JS expression can be embedded inside curly braces in JSX.