React常见问题

React常见问题

基础环境

操作系统 Ubuntu18.04

React问题

React 作为目前流行的三大Web前端开发框架之一,值得大家学习。本文主要记录在开发过程中遇到的各类问题。

在浏览器访问网页时时,提示“Something went wrong.”

react引用可视化组件recharts/ResponsiveContainer,报错Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it’s defined in, or you might have mixed up default and named imports.Check your code at Index.js:163. …

现象:Chrome浏览器访问时,提示Someting went wrong.
分析步骤:

  1. F12打开开发者工具,在Console中,显示如上信息
  2. 阅读自己编写的Index.js代码 <ResponsiveContainer.....//Line 163,发现问题是ResponsiveContainer组件,再观察引入recharts工具包的Component,包含以下方式:
import { CartesianGrid, Legend, Line, LineChart, Tooltip, XAxis, YAxis } from "recharts";
import ResponsiveContainer from "recharts/lib/component/ResponsiveContainer";

鼠标放在recharts,“转到定义F12”,发现node_modules/recharts/types/index.d.ts文件,包含ResponsiveContainer的相关export代码:

export { ResponsiveContainer } from './component/ResponsiveContainer';
export type { Props as ResponsiveContainerProps } from './component/ResponsiveContainer';

export { Line } from './cartesian/Line';
export type { Props as LineProps } from './cartesian/Line';

修改ResponsiveContainer组件的引入方式:

import { ResponsiveContainer,CartesianGrid, Legend, Line, LineChart, Tooltip, XAxis, YAxis } from "recharts";
  1. 进一步分析,此类问题产生原因:组件引入方式错误

控制台显示:Manifest: Line: 1, column: 1, Syntax error.错误

  1. 原因:react-app创建项目时,同事删除manifest.json文件。但index.html文件中包含manifest.json的引用。
  2. 措施:删除index.html文件中的<link rel="manifest" href="/manifest.json" />

控制台显示:mapStateToProps() in connect() must return a plain object, Instead received Undefined

  1. 分析问题:typescript const mapStateToProps = () => { }函数无返回结果
  2. 对策:修改以上代码,返回空结构体:typescript const mapStateToProps = () => ({ })或者const mapStateToProps = () => { return {} }

控制台显示:Warning: React does not recognize the tooltipTitle prop on a DOM element. If you intentionally want it to appear in the DOM as a custom attribute, spell it as lowercase tooltiptitle instead. If you accidentally passed it from a parent component, remove it from the DOM element.

控制台显示:Warning: Failed prop type: Invalid prop className of type array supplied to ForwardRef(SvgIcon), expected string.

  1. 分析问题:<VideoIcon className={[classes.iconFix]} />
  2. 删除[ ]代码变为<VideoIcon className={classes.iconFix} />

控制台提示:Warning: Failed prop type: Invalid prop autoComplete of type boolean supplied to ForwardRef(Input), expected string.

  1. 分析问题,控制台打印上述Warning,同时提示以下的调用堆栈。
 in ForwardRef(Input) (created by WithStyles(ForwardRef(Input)))
 in WithStyles(ForwardRef(Input)) (at LoginForm.js:294)
 ......

查阅代码LoginForm.js,确实存在<Input/>autoComplete
2.解决问题:删除<Input/>autoComplete属性
3.下一步:深入了解<Input/>的使用

控制台显示:Warning: Failed prop type: The prop justify of ForwardRef(Grid) is deprecated. Use justifyContent instead, the prop was renamed.

  1. 分析问题,控制台继续打印调用堆栈:
in ForwardRef(Grid) (created by WithStyles(ForwardRef(Grid)))
in WithStyles(ForwardRef(Grid)) (at Nav.js:76)
......

控制台显示Warning: Encountered two children with the same key, /#/home?path=%2F. Keys should be unique so that components maintain their identity across updates. Non-unique keys may cause children to be duplicated and/or omitted — the behavior is unsupported and could change in a future version.

  1. 分析问题,控制台继续打印调用堆栈:
 in div (created by ForwardRef(Grid))
 in ForwardRef(Grid) (created by WithStyles(ForwardRef(Grid)))
 in WithStyles(ForwardRef(Grid)) (at Nav.js:76)

控制台显示 Warning: validateDOMNesting(…):
cannot appear as a descendant of

.

  1. 分析问题,控制台继续打印调用堆栈:
in div (at FileList.js:219)

查看源码<ListItemText secondary={<div> </div>}/>;
2.处理方法:修改<div><span>
3.下一步搞清楚<div>不能使用的原因

网页无法正常打开,显示:redux TypeError: Cannot read property ‘apply‘ of undefined

  1. 分析问题(分析原因):在react的Web项目中,redux提供状态管理,开启调试模式;如果浏览器没有未安装react和redux相关的扩展程序,则会报以上的错误。
  2. 提出对策:
    情况1:在开发环节,安装浏览器的相关扩展程序( React Developer Tools)、(Redux DevTools);
    情况2:在发布环节,注释源码中相关调试代码,或者修改相关代码
// 文件名:index.js
// 注释代码
// window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()

//修改代码
window.__REDUX_DEVTOOLS_EXTENSION__    ?  window.__REDUX_DEVTOOLS_EXTENSION__()  :  (a) => a

Redux TypeError: Cannot read property ‘apply’ of undefined

yarn start Error: ENOSPC: System limit for number of file watchers reached

1.分析问题(分析原因):
2.提出对策

#Step 1 :打开编辑文件
sudo vim /etc/sysctl.conf

#Step2:在文件最后一行添加下面这句,并保存
fs.inotify.max_user_watches=524288

#Step3:执行
sudo sysctl -p
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React常见面试题包括但不限于以下几个方面: 1. React组件的生命周期:React组件的生命周期分为三个阶段,分别是挂载阶段、更新阶段和卸载阶段。在每个阶段,都有相应的生命周期方法可以被调用,比如componentDidMount、componentDidUpdate和componentWillUnmount等。 2. React的虚拟DOM和DOM diff算法:React通过使用虚拟DOM来提高性能。虚拟DOM是一个轻量级的JavaScript对象,它表示真实DOM的层次结构。当数据发生变化时,React会使用DOM diff算法来比较新旧虚拟DOM树的差异,并只更新需要变化的部分,从而避免了对整个DOM树进行重渲染。 3. React中的状态管理:React有多种状态管理的方式,最常用的是使用组件的stateprops来管理状态。另外,还可以使用React的Context API、Redux、Mobx等来实现更灵活的状态管理。 4. React中的事件处理:在React中,可以通过给元素添加事件处理函数来处理用户的交互行为。常见的事件处理方式包括直接在JSX中绑定事件处理函数、使用类组件中的成员方法作为事件处理函数、使用React的Hooks中的useEffect来处理副作用等。 5. React的组件通信:React组件之间的通信可以通过props传递数据、使用React的Context API、使用全局状态管理工具等方式实现。另外,如果组件之间的通信比较复杂,还可以考虑使用消息订阅-发布模式或者事件总线来实现。 6. React的性能优化:React提供了一些性能优化的手段,比如使用shouldComponentUpdate来控制组件的更新、使用React的memo和useMemo来缓存计算结果、使用React的Fragment来减少不必要的DOM节点等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值