React 笔记[1] hello world

React 笔记[1] hello world

明白了!既然你已经安装了 Node.js,我们可以 从零开始搭建一个 React + Tailwind CSS 的 Hello World 项目。我将一步步列出操作指令,你只需要在终端里依次执行。


✅ 第一步:初始化项目

bash 复制代码
mkdir my-hello-react
cd my-hello-react
npm init -y

✅ 第二步:安装 React 和 React DOM

bash 复制代码
# verbose 打印细节
npm install react react-dom --verbose

✅ 第三步:安装开发工具(Vite)

我们用 Vite 来做开发服务器和构建工具,它比 webpack 快得多:

bash 复制代码
npm install --save-dev vite --verbose

✅ 第四步:创建项目结构

在项目根目录下创建以下目录和文件:

bash 复制代码
mkdir src
touch index.html
touch vite.config.js
touch src/main.jsx
touch src/App.jsx

✅ 第五步:编辑文件内容

index.html
html 复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Hello React</title>
  </head>
  <body>
    <div id="root"></div>
    <script type="module" src="/src/main.jsx"></script>
  </body>
</html>
vite.config.js
js 复制代码
export default {
  root: '.',
};
src/main.jsx
jsx 复制代码
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import './index.css'; // 引入 Tailwind 样式

ReactDOM.createRoot(document.getElementById('root')).render(<App />);
src/App.jsx
jsx 复制代码
export default function App() {
  return (
    <div className="flex items-center justify-center h-screen bg-gray-100">
      <h1 className="text-4xl font-bold text-blue-600">Hello, World!</h1>
    </div>
  );
}

✅ 第六步:安装并配置 Tailwind CSS

bash 复制代码
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p
修改 tailwind.config.js
js 复制代码
export default {
  content: ["./index.html", "./src/**/*.{js,jsx}"],
  theme: {
    extend: {},
  },
  plugins: [],
};
创建并编辑 src/index.css
css 复制代码
@tailwind base;
@tailwind components;
@tailwind utilities;

✅ 第七步:添加运行脚本

package.jsonscripts 区块中添加:

json 复制代码
"scripts": {
  "dev": "vite",
  "build": "vite build",
  "preview": "vite preview"
}

✅ 第八步:运行项目!

bash 复制代码
npm run dev

打开浏览器访问 http://localhost:5173,你会看到 "Hello, World!" 的页面!


要我帮你打包成一个完整的 zip 文件结构,或者生成一个 GitHub 项目模板吗?

最佳实践

复制代码
 npm create vite@latest . -- --template react-ts
 # 选择 react
 # 选择 ts
相关推荐
崔庆才丨静觅5 小时前
hCaptcha 验证码图像识别 API 对接教程
前端
passerby60616 小时前
完成前端时间处理的另一块版图
前端·github·web components
掘了6 小时前
「2025 年终总结」在所有失去的人中,我最怀念我自己
前端·后端·年终总结
崔庆才丨静觅6 小时前
实用免费的 Short URL 短链接 API 对接说明
前端
wdfk_prog7 小时前
[Linux]学习笔记系列 -- [drivers][input]input
linux·笔记·学习
崔庆才丨静觅7 小时前
5分钟快速搭建 AI 平台并用它赚钱!
前端
ouliten7 小时前
cuda编程笔记(36)-- 应用Tensor Core加速矩阵乘法
笔记·cuda
崔庆才丨静觅7 小时前
比官方便宜一半以上!Midjourney API 申请及使用
前端
Moment7 小时前
富文本编辑器在 AI 时代为什么这么受欢迎
前端·javascript·后端
崔庆才丨静觅7 小时前
刷屏全网的“nano-banana”API接入指南!0.1元/张量产高清创意图,开发者必藏
前端