【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>,
)
相关推荐
_杨瀚博6 分钟前
微信支付集成_JSAPI
前端
polaris_tl6 分钟前
react beginwork
前端
亮子AI16 分钟前
【css】列表的标号怎么实现居中对齐?
前端·css
梦想的旅途241 分钟前
媒体文件(图片/文件)的上传与管理:获取 Media ID 的技术细节
前端·http·servlet
一水鉴天1 小时前
整体设计 定稿 之22 dashboard.html 增加三层次动态记录体系仪表盘 之1
前端·html
张拭心1 小时前
程序员越想创业,越不要急着动手
前端·人工智能
舒一笑1 小时前
在低配云服务器上实现自动化部署:Drone CI + Gitee Webhook 的轻量级实践
前端·后端·程序员
龙国浪子1 小时前
从零到一:打造专业级小说地图设计工具的技术实践
前端·electron
一水鉴天2 小时前
整体设计 定稿 之24+ dashboard.html 增加三层次动态记录体系仪表盘 之2 程序 (Q208 之2)
开发语言·前端·javascript
IT_陈寒2 小时前
Java 21新特性实战:这5个改进让我的代码效率提升40%
前端·人工智能·后端