1.创建项目
npm i -g create-react-app
create-react-app react-demo
//创建js版项目
npx create-react-app react-demo
//创建ts版项目
npx create-react-app react-demo --template=typescript
2.public文件夹
index.html
<!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" />
<!-- mainfest.json做应用加壳配置文件 -->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!-- 标题 -->
<title>React App</title>
</head>
<body>
<!-- 浏览器不支持js运行 -->
<noscript>You need to enable JavaScript to run this app.</noscript>
<!-- 根标签 -->
<div id="root"></div>
</body>
</html>
3.src文件夹
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
ReactDOM.render(
// React.StrictMode检查app和app包裹的组件是否合理
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
//记录页面的性能
reportWebVitals();
setupTests.js
//检查测试
import '@testing-library/jest-dom';
reportWebVitals.js
记录性能
const reportWebVitals = onPerfEntry => {
if (onPerfEntry && onPerfEntry instanceof Function) {
import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => {
getCLS(onPerfEntry);
getFID(onPerfEntry);
getFCP(onPerfEntry);
getLCP(onPerfEntry);
getTTFB(onPerfEntry);
});
}
};
export default reportWebVitals;