React 入门第一步:环境搭建

开发 前端
React 具有声明式、组件化、一次学习,随处编写等特点,使用 React 可以将一些简短、独立的代码片段组合成复杂的 UI 界面,这些代码片段被称作“组件”。

[[419259]]

React 是 facebook 推出的用于构建用户界面的前端 Javascript 库,中文官方地址为:https://react.docschina.org/。

React 具有声明式、组件化、一次学习,随处编写等特点,使用 React 可以将一些简短、独立的代码片段组合成复杂的 UI 界面,这些代码片段被称作“组件”。

环境搭建

官方文档中创建新的 React 应用:https://react.docschina.org/docs/create-a-new-react-app.html

手动搭建 webpack

创建项目目录并安装开发依赖:

  1. $ mkdirwebpack-react-project   
  2.  
  3. $ cd webpack-react-project/  
  4.  
  5. $ npm init -y  
  6.  
  7. npm install -D @babel/core@7.13.8@babel/preset-env@7.13.9 @babel/preset-react@7.12.13 babel-loader@8.2.2html-webpack-plugin@4.5.2 react@17.0.1 react-dom@17.0.1 webpack@4.46.0webpack-cli@3.3.12 webpack-dev-server@3.11.2 
  1. + react@17.0.1 
  2. + babel-loader@8.2.2 
  3. + @babel/preset-env@7.13.9 
  4. + webpack-dev-server@3.11.2 
  5. + @babel/core@7.13.8 
  6. + html-webpack-plugin@4.5.2 
  7. + webpack-cli@3.3.12 
  8. + @babel/preset-react@7.12.13 
  9. + react-dom@17.0.1 
  10. + webpack@4.46.0 

项目创建完成,开发依赖安装成功后,package.json 内容如下:

  1.   "name":"webpack-react-project"
  2.   "version":"1.0.0"
  3.   "description":""
  4.   "main":"index.js"
  5.   "scripts":{ 
  6.     "test":"echo \"Error: no test specified\" && exit 1" 
  7.   }, 
  8.   "keywords":[], 
  9.   "author":""
  10.   "license":"ISC"
  11.   "devDependencies":{ 
  12.     "@babel/core":"^7.13.8"
  13.     "@babel/preset-env":"^7.13.9"
  14.     "@babel/preset-react":"^7.12.13"
  15.     "babel-loader":"^8.2.2"
  16.     "html-webpack-plugin":"^4.5.2"
  17.     "react":"^17.0.1"
  18.     "react-dom":"^17.0.1"
  19.     "webpack":"^4.46.0"
  20.     "webpack-cli":"^3.3.12"
  21.     "webpack-dev-server":"^3.11.2" 
  22.   } 

因为是自己进行手动安装配置,因此需要在项目根路径下手动创建 \webpack.config.js 文件,并做如下配置:

  1. const path =require('path'
  2. const HtmlWebpackPlugin =require('html-webpack-plugin'
  3.  
  4. module.exports= { 
  5.   mode:'development'
  6.   devtool:'none'
  7.   entry:'./src/index.js'
  8.   output: { 
  9.     filename:'main.js'
  10.     path: path.resolve('dist'
  11.   }, 
  12.   devServer: { 
  13.     port:3000, 
  14.     hot:true 
  15.   }, 
  16.   module: { 
  17.     rules: [ 
  18.       { 
  19.         test:/\.js|jsx$/, 
  20.         exclude:/node_modules/, 
  21.         use: [ 
  22.           { 
  23.             loader:'babel-loader'
  24.             options: { 
  25.               presets: ['@babel/preset-env','@babel/preset-react'
  26.             } 
  27.           } 
  28.         ] 
  29.       } 
  30.     ] 
  31.   }, 
  32.   plugins: [ 
  33.     newHtmlWebpackPlugin({ 
  34.       template:'./src/index.html' 
  35.     }) 
  36.   ] 

配置入口 \src\index.html

  1. <body> 
  2.   <divid="root"></div> 
  3. </body> 

配置入口 \src\index.js

  1. import React from'react' 
  2. import { render } from'react-dom' 
  3.  
  4. // 自定义组件 
  5. functionApp() { 
  6.   return<div>React</div> 
  7.  
  8. render(<App />,document.getElementById('root')) 

然后在 package.json 中添加配置选项:

  1. "scripts":{ 
  2.     "test":"echo \"Error: no test specified\" && exit 1"
  3.     "dev":"webpack-dev-server" 
  4.   }, 

然后在命令行中执行 npm run dev 就可以启动项目了。

使用官方脚手架create-react-app

官方脚手架 create-react-app 基于 webpack 进行打包构建。

脚手架构架项目:npx create-react-appmy-app

  • > cd my-app
  • > npm start

使用通用构建工具 Vite

Vite 本身就是一个构建工具,开发环境下不打包,生成环境使用 Rollup 进行打包。

执行命令 npm init vite@latest

  1. √ Project name: ...my-project 
  2. Select a framework: » - Use arrow-keys. Return tosubmit.   
  3.     vanilla 
  4.     vue 
  5.  >  react 
  6.     preact 
  7.     lit-element 
  8.     svelte 
  9. Select a variant: » - Use arrow-keys. Return tosubmit. 
  10. >   react 
  11.     react-ts 
  12.  
  13. Scaffolding project in xxxxxxxxxxxxxx 
  14.  
  15. Done. Now run:   
  16.   cdvite-project 
  17.   npm install 
  18.   npm run dev 
  19.   

 

责任编辑:武晓燕 来源: 勾勾的前端世界
相关推荐

2021-01-15 18:17:06

网络协议分层

2009-01-18 08:49:04

Java入门JDK

2012-07-11 16:43:14

飞视美

2013-01-15 09:17:11

2015-06-02 11:42:00

Cloud FoundAzure

2018-02-10 11:24:39

Python数据程序

2019-11-20 10:54:46

无密码身份验证网络安全

2020-11-17 14:55:36

亚马逊云科技迁移

2011-07-25 14:17:46

BSMIT运维北塔

2010-07-01 13:44:12

2010-01-21 10:29:54

java认证

2012-08-30 11:14:11

云计算虚拟化

2020-07-22 22:10:34

互联网物联网IOT

2009-06-16 16:31:08

绿色IT数据中心游龙科技

2011-08-31 09:38:36

网络营销MSN社交网络

2020-11-11 07:09:05

隔离直播系统

2010-11-05 10:32:50

云应用程序规划

2021-09-30 16:05:04

显卡虚拟货币芯片

2017-09-19 09:36:55

思科服务

2013-04-03 09:22:14

虚拟化网络虚拟化
点赞
收藏

51CTO技术栈公众号