A. create-react-app4和5的区别
create-react-app是一种快速构建React应用程序的工具,可以自动生成开发环境和配置文件。create-react-app的版本从4.0开始,到目前的5.2。
create-react-app 4.0和5.0之间的主要区别如下:
支持的React版本不同:create-react-app 4.0支持React 16.9及以下版本,而create-react-app 5.0支持React 17及以上版本。
新增的脚本命令:create-react-app 5.0新增了一些脚本命令,如npm start --inspect-brk,用于调试应用程序。
默认使用Webpack 5:create-react-app 5.0默认使用Webpack 5,而create-react-app 4.0使用的是Webpack 4。
B. React新旧版本对比总结归纳(生命周期篇)
我们先来看下React16.0前后生命周期变化的图片
生命周期图谱: http://projects.wojtekmaj.pl/react-lifecycle-methods-diagram/
① componentWillMount
② componentWillReceiveProps
③ componentWillUpdate
① UNSAFE_componentWillMount
② UNSAFE_componentWillReceivePorps
③ UNSAFE_componentWillUpdate
① getDerivedStateFromProps
② getSnapShotBeforeUpdate
③ getDerivedStateFromError
④ componentDidCatch
○ 什么时候调用?
这个方法会在调用 render 方法之前调用,并且在初始挂载及后续更新时都会被调用。它应返回一个对象来更新 state,如果返回 null 则不更新任何内容。
○ 返回值
返回值是必须的,它应该是一个对象,用于更新state; 如果返回值为null,state不更新。
○ 主要用途?
这个生命周期函数是为了替代componentWillReceiveProps存在的,所以在你需要使用componentWillReceiveProps的时候,就可以考虑使用getDerivedStateFromProps来进行替代。主要是将新的props更新到state上
看下边例子就明白了!
○ 什么时候调用?
getSnapshotBeforeUpdate() 在最近一次渲染输出(提交到 DOM 节点)之前调用。
○ 返回值
应返回 snapshot 的值(或 null)
○ 有什么用途?
它使得组件能在发生更改之前从 DOM 中捕获一些信息(例如,滚动位置)。此生命周期的任何返回值将作为参数传递给 componentDidUpdate()。
此用法并不常见,但它可能出现在 UI 处理中,如需要以特殊方式处理滚动位置的聊天线程等。
○ 什么时候调用?
此生命周期会在渲染阶段后代组件抛出错误后被调用,因此不允许出现副作用。
○ 返回值
它将抛出的错误作为参数,并返回一个值以更新 state
○ 有什么用途?
主要用于封装错误边界用,收集错误信息并做对应处理。
以下为具体用法用例
然后你可以将它作为一个常规组件去使用:
○ 什么时候调用?
此生命周期会在“提交”阶段后代组件抛出错误后被调用,因此允许执行副作用。
○ 参数
error —— 抛出的错误。
info —— 带有 componentStack key 的对象,其中包含有关组件引发错误的栈信息。
○ 返回值
它将抛出的错误作为参数,并返回一个值以更新 state
○ 有什么用途?
主要用于封装错误边界用,收集错误信息并做对应处理。
用例如下:
结论:对于新生命周期③和④作用基本是一致的,都是用于封装错误边界,收集边界下后代组件构造函数中发生的错误信息并作出对应处理。不同的是调用时间和返回参数。
C. 关于React升级版本后造成白屏
react从16升级到17版本后,npm run start 本地跑的时候散唤迹没有问题,但是一旦build部署上去,就会白屏,然后出现这个报错
点开一看,卡在冲并这里
查阅后得知,react从16升级到17后,相链埋应的react-dom也要升级到17,
输入 npm i react-dom@17 --save 已解决
D. 如何管理React-Native库的版本
首先安装了nodejs 4.乏订催寡诎干挫吮旦经1 版本
然后 安装了 npm install -g react-native-cli 模块
安装 android sdk 并且配置android 环境变量
安装gradle 环境
进入 这个博客 里边有 react-native for android 项目,下载下来,然后执行 npm install 先安装 react 依赖模块包
打开两个 命令窗口 1. 一个 执行react-native start ,另一个执行react-native run-android 就可以了
E. 怎么看react build是什么版本
安装node.js 由于nodejs中使用GYP进行项目构建管理,而GYP是基于Python的构建工具。因此,需要先安装Python。此外,在Windows系统下,nodejs采用GYP来生成Visual Studio Solution文件后,需要通过C++的编译器将其编译为二进制文件。因此,又需要先安装C++编译环境。最后,安装nodejs【墙裂建议选择stable稳定版】。一系列安装完成后,进入cmd,输入"npm"指令后回车,出现形同以下画面内容,则安装成功。 需要配置path,加入如:E:\Android\Python27;C:\Users\che\AppData\Roaming\npm;
F. react native gradle怎么升级
更新版本的React Native将会让你访问更多的APIs, 这样我们可以去使用更多的APIs,视图Views,以及开发者工具以及其他一些好用的东西。浏览版本更新日志就会发现整体FB官方开发进度还是非常紧张的,这样就很容易导致旧版本会出现不能用的情况,后期可能会出现不可预料的情况,这边建议大家尽量更新最新正式版本。
我们大家都知道一个React Native项目使用Android项目,iOS项目和JavaScript项目三部分进行组成的,而且三者都关联打包在npm包内的,所以该项目版本进行更新是比较繁琐的。下面我来给大家讲解一下React Native项目的升级步骤。
一、查看当前版本
[plain] view plain
<span style="font-family:SimSun;font-size:14px;">pengchengxiang@ubuntu:~/AwesomeProject$ react-native --version
react-native-cli: 0.2.0
react-native: 0.22.2</span>
二、查看最新的版本
[plain] view plain
<span style="font-family:SimSun;font-size:14px;">pengchengxiang@ubuntu:~/AwesomeProject$ npm info react-native
{ name: 'react-native',
'dist-tags': { latest: '0.25.1', next: '0.26.0-rc' },
versions:
[ '0.0.0',
'0.0.5',
... ...
'0.24.1',
'0.25.0-rc',
'0.25.1',
'0.26.0-rc' ],
maintainers:
... ...
dist:
{ shasum: '',
tarball: 'https://registry.npmjs.org/react-native/-/react-native-0.25.1.tgz' },
directories: {} }</span>
三、升级依赖的版本
[plain] view plain
<span style="font-family:SimSun;font-size:14px;">pengchengxiang@ubuntu:~/AwesomeProject$ npm install --save [email protected]
> [email protected] install /home/pengchengxiang/AwesomeProject/node_moles/react-native/node_moles/ws/node_moles/bufferutil
> node-gyp rebuild
make: Entering directory `/home/pengchengxiang/AwesomeProject/node_moles/react-native/node_moles/ws/node_moles/bufferutil/build'
CXX(target) Release/obj.target/bufferutil/src/bufferutil.o
SOLINK_MODULE(target) Release/obj.target/bufferutil.node
COPY Release/bufferutil.node
make: Leaving directory `/home/pengchengxiang/AwesomeProject/node_moles/react-native/node_moles/ws/node_moles/bufferutil/build'
> [email protected] install /home/pengchengxiang/AwesomeProject/node_moles/react-native/node_moles/ws/node_moles/utf-8-validate
> node-gyp rebuild
make: Entering directory `/home/pengchengxiang/AwesomeProject/node_moles/react-native/node_moles/ws/node_moles/utf-8-validate/build'
CXX(target) Release/obj.target/validation/src/validation.o
SOLINK_MODULE(target) Release/obj.target/validation.node
COPY Release/validation.node
make: Leaving directory `/home/pengchengxiang/AwesomeProject/node_moles/react-native/node_moles/ws/node_moles/utf-8-validate/build'
> [email protected] postinstall /home/pengchengxiang/AwesomeProject/node_moles/react-native/node_moles/yeoman-generator/node_moles/cross-spawn/node_moles/spawn-sync
> node postinstall
npm WARN optional dep failed, continuing [email protected]
[email protected] node_moles/react-native
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── word[email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected] ([email protected])
├── [email protected] ([email protected])
├── [email protected] ([email protected])
├── [email protected] ([email protected])
├── [email protected] ([email protected], [email protected])
├── [email protected] ([email protected], [email protected], [email protected], [email protected], [email protected])
├── [email protected] ([email protected])
├── [email protected] ([email protected])
├── [email protected]
├── [email protected] ([email protected], [email protected])
├── [email protected] ([email protected], [email protected])
├── [email protected] ([email protected])
├── [email protected] ([email protected], [email protected])
├── [email protected] ([email protected], [email protected], [email protected], [email protected], [email protected], [email protected])
├── [email protected] ([email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected])
├── [email protected] ([email protected], [email protected])
├── [email protected]
├── [email protected] ([email protected], [email protected], [email protected], [email protected])
├── [email protected]
├── [email protected] ([email protected], [email protected], [email protected], [email protected])
├── [email protected] ([email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected])
├── [email protected] ([email protected], [email protected], [email protected], [email protected])
├── [email protected] ([email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected])
├── [email protected] ([email protected])
├── [email protected] ([email protected], [email protected], [email protected], [email protected])
├── [email protected] ([email protected], [email protected])
├── [email protected] ([email protected], [email protected], [email protected], [email protected])
├── [email protected] ([email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected])
├── [email protected] ([email protected])
├── [email protected] ([email protected], [email protected], [email protected], [email protected])
├── [email protected]
├── [email protected] ([email protected], [email protected], [email protected], [email protected])
├── [email protected] ([email protected], [email protected], [email protected], [email protected], [email protected])
├── [email protected]
├── [email protected] ([email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected])
├── [email protected] ([email protected], [email protected])
├── [email protected] ([email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected])
└── [email protected] ([email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected])</span>