【React】从零开始搭建 react 项目(初始化+路由)

创建 React 项目

创建项目的方式:create-react-app 项目名称

如果没有安装 react 脚手架,请先安装

shell 复制代码
npm isntall create-react-app -g

安装成功后,开始配置项目

配置项目的 icon 和标题

配置 jsconfig.json

目的:用于配置 JavaScript 或 TypeScript 项目的根目录和其他一些选项,以帮助编辑器(如 VS Code)更好地理解项目的结构和提供智能提示

更多可以查看【React 】react 创建项目配置 jsconfig.json 的作用

js 复制代码
{
  "compilerOptions": {
    "target": "es5",
    "module": "esnext",
    "baseUrl": "./",
    "moduleResolution": "node",
    "paths": {
      "@/*": [
        "src/*"
      ]
    },
    "jsx": "preserve",
    "lib": [
      "esnext",
      "dom",
      "dom.iterable",
      "scripthost"
    ]
  }
}

配置 craco.config.js

CRACO(Create React App Configuration Override)是一个易于理解且强大的工具,它提供了一种简单的方法来覆盖和自定义 Create React App 的配置,而无需执行"eject"操作。这种方法让你能够保留 Create React App 提供的所有优势,同时还能根据需要修改配置。

目的:用于配置 webpack 和一些 react 插件的配置

shell 复制代码
npm i -D @craco/craco craco-less

安装 craco-less 主要是为了使用 less

js 复制代码
const path = require('path')
const CracoLessPlugin = require('craco-less');

const resolve = pathname => path.resolve(__dirname, pathname)

module.exports = {
  // less
  plugins: [
    {
      plugin: CracoLessPlugin
    },
  ],
  // webpack
  webpack: {
    alias: {
      "@": resolve("src"),
      "components": resolve("src/components"),
      "utils": resolve("src/utils")
    }
  }
}

接下来,你需要更新 package.json 中的 scripts 部分,将原本调用 react-scripts 的命令替换为使用 CRACO CLI

json 复制代码
"scripts": {
  "start": "craco start",
  "build": "craco build",
  "test": "craco test"
}

项目目录结构划分

清理 react 脚手架默认创建的文件,重新创建符合个人项目开发的文件夹结构(参考如下)

CSS 样式重置

安装 normalize.css

shell 复制代码
npm install normalize.css

导入

自己编写其他的样式重置 css

css 复制代码
body, button, dd, dl, dt, form, h1, h2, h3, h4, h5, h6, hr, input, li, ol, p, pre, td, textarea, th, ul {
  padding: 0;
  margin: 0;
}

a {
  text-decoration: none;
}


img {
  vertical-align: top;
}

ul, li {
  list-style: none;
}

Router 配置

shell 复制代码
npm install react-router-dom

src\router\index.js

js 复制代码
import React from "react"
import { Navigate } from "react-router-dom"

// 懒加载
const Home = React.lazy(() => import("@/views/home"))
const Entire = React.lazy(() => import("@/views/entire"))
const Detail = React.lazy(() => import("@/views/detail"))

const routes = [
  {
    path: "/",
    element: <Navigate to="/home"/>
  },
  {
    path: "/home",
    element: <Home/>
  },
  {
    path: "/entire",
    element: <Entire/>
  },
  {
    path: "/detail",
    element: <Detail/>
  }
]

export default routes

App.jsx

js 复制代码
import { memo } from 'react';
import routes from './router';
import { useRoutes } from 'react-router-dom';

// memo: 组件的渲染结果,只会在 props 改变时才会重新渲染
const App = memo(() => {
  return (
    <div className="app">
      <div className="header">header</div>
      <div className="page">{useRoutes(routes)}</div>
      <div className="footer">footer</div>
    </div>
  );
});

export default App;

index.js

js 复制代码
import React, { Suspense } from 'react' // Suspense 解决路由懒加载问题
import ReactDOM from 'react-dom/client'
import { HashRouter } from 'react-router-dom'

import App from '@/App'
import 'normalize.css'
import './assets/css/index.less'


const root = ReactDOM.createRoot(document.getElementById('root'))
root.render(
  <Suspense fallback='loading'>
    <HashRouter>
      <App />
    </HashRouter>
  </Suspense>,
)
相关推荐
Csvn3 小时前
性能监控
前端
Csvn3 小时前
构建工具
前端
FRing034 小时前
vue2干货总结3(computed计算属性、watch监听器)
开发语言·前端·vue.js·vue2
kyriewen6 小时前
Skills 榜前 10 我全装了:只有 3 个能直接用
前端·javascript·程序员
IT_陈寒6 小时前
当Java的hashCode遇上Lombok,我的缓存被全清了
前端·人工智能·后端
工控丫丫6 小时前
帆软看板中经常使用的公式函数及经常搭的组合
前端·javascript·数据库
jerrywus7 小时前
我用 GPT-6.1-sol 做了台会讲解的 3D 售货机:60 罐饮料,一口没喝上
前端·agent·claude
孟陬8 小时前
Foobar 技术周刊#159:当看不懂代码时,如何高效排障调试。脏页陷阱:一条只读查询,写入数 TB 数据。推测解码:大模型加速利器
前端·人工智能·后端
Zhao-Jichao8 小时前
AI Prompting for Everyone 课程笔记 - P17 图像生成 Imaging Generation
前端
Highcharts.js9 小时前
智能数据分析组件Orbit 正式发布与快速上手体验教程
前端·人工智能·信息可视化·数据分析系统·highcharts·orbit·智能可视化