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
相关推荐
我不吃饼干7 小时前
在 React 中实现倒计时功能会有什么坑
前端·react.js
小小小小宇8 小时前
前端PerformanceObserver
前端
王者鳜錸8 小时前
PYTHON从入门到实践-18Django从零开始构建Web应用
前端·python·sqlite
拾光拾趣录8 小时前
ES6到HTTPS全链路连环拷问,99%人第3题就翻车?
前端·面试
haaaaaaarry9 小时前
Element Plus常见基础组件(二)
开发语言·前端·javascript
Hello_Embed9 小时前
嵌入式 C 语言入门:循环结构学习笔记 —— 从语法到实用技巧
c语言·笔记·stm32·学习
xyphf_和派孔明9 小时前
关于echarts的性能优化考虑
前端·性能优化·echarts
PyHaVolask9 小时前
HTML 表单进阶:用户体验优化与实战应用
前端·javascript·html·用户体验
A了LONE9 小时前
cv弹窗,退款确认弹窗
java·服务器·前端
AntBlack10 小时前
闲谈 :AI 生成视频哪家强 ,掘友们有没有推荐的工具?
前端·后端·aigc