【react】初学react5-react脚手架搭建中的小众知识

文章目录


前言

终于到脚手架了,真正的react才刚刚开始!握拳.jpg


一、环境下载

安装脚手架:

npm i create-react-app

vscode终端中创建项目:(我的环境下npm create-react-app my-app会报错)

npx create-react-app my-app

安装react插件,这里推荐ES7 React/Redux/GraphQL/React-Native snippets,内置非常多的快捷代码片段,在细节中可以查看,比如'rcc'可以快速生成react组件模版代码。

二、项目结构

网上可以看到的很多关于react项目结构和各文件作用的文章,这里不在做详细解释,只分享作者了解到的一些新知识和重点内容。

react中js jsx文件都可以省略后缀名

public-index.html文件,head中许多配置可以删除,代码及解释如下:

javascript 复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <!-- %PUBLIC_URL% 代表public文件夹路径 -->
    <link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
    <!-- 用于开启理想视口,做移动端网页适配 -->
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <!-- 配置浏览器页签+地址栏的颜色,仅适用于手机安卓系统 -->
    <meta name="theme-color" content="#000000" />
    <meta
      name="description"
      content="Web site created using create-react-app"
    />
    <!-- 用于指定网页添加到手机主屏幕后的图标 -->
    <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
    <!--
      manifest.json provides metadata used when your web app is installed on a
      user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
    -->
      <!-- 应用加壳时的配置文件 -->
    <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
    <title>React App</title>
  </head>
  <body>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>

  </body>
</html>

根目录下:index.js是入口文件,用ReactDOM.render(document.getElementById('root'))挂载这种写法也可,但新版好像不行了。

javascript 复制代码
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';



const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  // React.StrictMode 严格模式
  <React.StrictMode>
    <App />
  </React.StrictMode>
);

// reportWebVitals 记录页面性能
reportWebVitals();

关于样式

一般会引入less、sass去写样式,如果直接写css样式,要注意不同组件中的作用域,最好是采用模块化的样式:即 .css文件名改为.xx.module.css,模块化引入:

import hello from './Hello.module.css'

调用:className={hello.title}


总结

下篇分享一个代办事项的经典案例

相关推荐
清晨Coding29 分钟前
为什么用 React Developer Tools 查看组件树这么乱?
前端·react.js
鶴哥只手遮天1 小时前
《Electron 45.2.3 内核解剖》第 4 章 补丁体系:380 个补丁改了什么
前端·electron
FungLeo3 小时前
成为全栈·Vue 管理后台篇·分类树与标签管理:层级数据如何落到管理界面
前端·javascript·vue.js·分类树·naive ui·标签管理·前端数据结构
Lank_M6 小时前
白底都占八成,PNG 为何只在图表页赢过 JPG
前端
张一凡936 小时前
给 Coding Agent 加一道"架构闸门":Jin 是如何做到"先审架构、再写代码、不漂移"的
前端·后端·agent
Linye6 小时前
AI 写的 HTML 原型全是空壳?聊聊真实 DOM 交互避坑
前端·人工智能
ZANE7 小时前
纯前端实现的 PDF 与图片互相转换工具技术实现
前端
Swot7 小时前
SvelteKit Remote Functions 使用指南
前端·后端
Cooper257 小时前
三源热榜去重加 AI 摘要:URL 唯一键踩出的三个坑
前端·python
Yk酱228407 小时前
A2UI探索随笔-技术选型
前端