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
相关推荐
Hooray30 分钟前
为了在 Vue 项目里用上想要的 React 组件,我写了这个 skill
前端·ai编程
咸鱼翻身了么31 分钟前
模仿ai数据流 开箱即用
前端
风花雪月_32 分钟前
🔥IntersectionObserver:前端性能优化的“隐形监工”
前端
Bigger32 分钟前
告别 AI 塑料感:我是如何用 frontend-design skill 重塑项目官网的
前端·ai编程·trae
发际线向北32 分钟前
0x02 Android DI 框架解析之Hilt
前端
Ruihong32 分钟前
Vue v-bind 转 React:VuReact 怎么处理?
vue.js·react.js·面试
zhensherlock1 小时前
Protocol Launcher 系列:Overcast 一键订阅播客
前端·javascript·typescript·node.js·自动化·github·js
liangdabiao1 小时前
开源AI拼豆大升级 - 一键部署cloudflare page - 全免费 web和小程序
前端·人工智能·小程序
SuperHeroWu72 小时前
【鸿蒙基础入门】概念理解和学习方法论说明
前端·学习·华为·开源·harmonyos·鸿蒙·移动端
Full Stack Developme2 小时前
MyBatis-Plus 流式查询教程
前端·python·mybatis