React报错之Expected an assignment or function call and instead saw an expression
正文从这开始~
总览
当我们忘记从函数中返回值时,会产生"Expected an assignment or function call and instead saw an expression"错误。为了解决该错误,确保显式地使用return语句或使用箭头函数隐式返回。
下面有两个示例来展示错误是如何产生的。
// App.js
const App = props => {
const result = ['a', 'b', 'c'].map(el => {
// ️ Expected an assignment or function call and instead saw an expression. eslint no-unused-expressions
el + '100';
});
return <div>hello world</div>;
};
const mapStateToProps = (state) => {
// ️ Expected an assignment or function call and instead saw an expression. eslint no-unused-expressions
todos: ['walk the dog', 'buy groceries']
}
export default App;
在App组件中,错误是在Array.map()方法中引起的。这里的问题在于,我们没有从传递给map()方法的回调函数中返回任意值。
在JavaScript函数中,如果我们没有显式地使用
return语句,或者使用箭头函数隐式地返回一个值,则返回undefined。
mapStateToProps函数中的问题是一样的,我们忘记从函数中返回值。
显式返回
为了解决该错误,我们必须显式地使用return语句或使用箭头函数隐式返回值。
下面是一个例子,用来说明如何使用显式return来解决这个错误。
const App = props => {
const result = ['a', 'b', 'c'].map(el => {
return el + '100'; // ️ using explicit return
});
console.log(result);
return <div>hello world</div>;
};
const mapStateToProps = state => {
return {todos: ['walk the dog', 'buy groceries']}; // ️ using explicit return
};
export default App;
我们通过在map()方法中显式返回来解决问题。这是必须的,因为Array.map方法返回一个数组,其中包含我们传递给它的回调函数所返回的所有值。
需要注意的是,当你从一个嵌套函数中返回时,你并没有同时从外层函数中返回。
隐式返回
另一种方法是使用箭头函数的隐式返回。
// ️ implicit return
const App = props => (
<div>
<h2>hello</h2>
<h2>world</h2>
{['a', 'b', 'c'].map(element => (
<div key={element}>{element}</div>
))}
</div>
);
// ️ implicit return
const result = ['a', 'b', 'c'].map(element => element + '100');
console.log(result); // ️ ['a100', 'b100', 'c100']
// ️ implicit return
const mapStateToProps = state => ({
todos: ['walk the dog', 'buy groceries'],
});
export default App;
我们为App组件使用了隐式地箭头函数返回。
需要注意的是,我们根本没有使用大括号。简短的隐式返回使用圆括号。
返回对象
如果我们使用隐式返回来返回一个对象,我们必须用圆括号来包裹这个对象。
// RIGHT
const mapStateToProps = state => ({
todos: ['walk the dog', 'buy groceries'],
});
// ️ WRONG
const msp = state => {
// ️ Expected an assignment or function call and instead saw an expression.eslint no-unused-expressions
todos: ['walk the dog', 'buy groceries']
};
一个简单的思考方式是--当你使用大括号而没有用圆括号包裹它们时,你是在声明一个代码块(比如if语句)。
{
console.log('this is my block of code');
}
当不使用圆括号时,你有一个代码块,而不是一个对象。
但当你用圆括号包裹住大括号时,你就有一个隐式的箭头函数返回。
如果你认为eslint规则不应该在你的方案中造成错误,你可以通过使用注释来关闭某一行的eslint规则。
// eslint-disable-next-line no-unused-expressions
注释应该放在造成错误的那一行的正上方。
React报错之Expected an assignment or function call and instead saw an expression的更多相关文章
- React报错之Expected `onClick` listener to be a function
正文从这开始~ 总览 当我们为元素的onClick属性传递一个值,但是该值却不是函数时,会产生"Expected onClick listener to be a function" ...
- react报错this.setState is not a function
当报错这个的时候就要看函数是否在行内绑定this,或者在constructor中绑定this. 我这里犯的错误的是虽然我在constructor中绑定了this,但是语法写的不正确. 错误示范: co ...
- React报错之map() is not a function
正文从这开始~ 总览 当我们对一个不是数组的值调用map()方法时,就会产生"TypeError: map is not a function"错误.为了解决该错误,请将你调用ma ...
- 读取导入csv csv报错iterable expected, not float
示例代码import pandas as pdimport reimport csv data = pd.read_csv('nuojia.csv', encoding='utf-8')# print ...
- react 报错的堆栈处理
react报错 Warning: You cannot PUSH the same path using hash history 在Link上使用replace 原文地址https://reactt ...
- selenium调用Firefox和Chrome需要注意的一些问题,和出现的报错selenium:expected [object undefined] undefined to be a string
在高版本selenium下如:selenium3.4.3 1.高版本的selenium需要浏览器安装一些补丁驱动 Firefox:geckodriver 下载网址:http://download.cs ...
- 前端控制台 JavaScript函数报错 SyntaxError: expected expression, got ';' SyntaxError: expected expression, got 'if'
在火狐浏览器下调试时, 页面报错SyntaxError: expected expression, got ';'或者SyntaxError: expected expression, got 'if ...
- JavaScript函数报错SyntaxError: expected expression, got ';'
故事背景:编写Javaweb项目,在火狐浏览器下运行时firebug报错SyntaxError: expected expression, got ';'或者SyntaxError: expected ...
- myeclipse 10 载入新的项目报错Cannot return from outside a function or method
myeclipse 10 载入新的项目报错Cannot return from outside a function or method 解决方法: 方法一: window -->prefere ...
随机推荐
- 【Java面试】说说你对Spring MVC的理解
一个工作了7年的粉丝,他说在面试之前,Spring这块的内容准备得很充分. 而且各种面试题也刷了,结果在面试的时候,面试官问:"说说你对Spring MVC的理解". 这个问题一下 ...
- 解决python 导入selenium 库后自动化运行成功但是报错问题
本章节开始进入自动化的基础教学了,首先我们要对我们的工具有一定的熟练使用程度,做自动化常用的工具一个是搭建 RobotFramework自动化框架,另外一个便是我们最常用的python 工作原理是比较 ...
- 互联网大厂目标管理OKR实践落地与反思
上一篇「 互联网公司目标管理OKR和绩效考核的误区 」介绍了使用 OKR 时要澄清的一些概念,但是实际使用中又如何呢?我们快手也是很大的互联网公司,大家都是年轻人,思维活跃,容易接受新事物,敢尝试,但 ...
- ubuntu使用postfix和AWS-SES发送邮件
在日常开发中,邮件发送是个比较常见的场景.因此出现了很多相关的软件和服务,各大云厂商也推出自己的邮件服务.今天笔者就像大家介绍一种常见的组合,AWS的邮件服务 SES 与邮件服务器 postfix 的 ...
- 网心云在PVE下三种磁盘IO模式(No cache,Write through,Write back)选择与优化指南
---------------------------------------------------------------------------------------------------- ...
- 【Github】 Github修改仓库的基本信息
前言 我们通常在刚开始了解学习使用github时,一般都是测试的使用,有时我们向里面添加了一些代买,如果想要修改信息并且是删除仓库重新创建提交,可以采用下面方法修改仓库信息,名称.描述等. 修改仓库描 ...
- 下载nltk数据包报错
安装nltk需要两步:安装nltk和安装nltk_data数据包 安装nltk 安装nltk很简单,可以直接在pycharm环境中安装,flie -> settings-> Python ...
- iftop使用
在linux中监控系统资源.进程.内存占用等信息,可以使用top命令. 查看网络状态可以使用netstat工具. 如果想查看实时的网络流量,监控TCP/IP连接等,则可以使用iftop工具. 一.if ...
- Linux安装fastdfs集群部署
过程问题: make: gcc:命令未找到 解决: yum -y install gcc 一.环境和版本: Linux环境:CentOS 7.6 libfastcommon版本:1.0.39 Fast ...
- C语言动态输出等腰三角形
C语言动态输出等腰三角形 题目要求:输入行数 打印出对应行数的等腰三角形,要求使用for循环嵌套. 思路 while语句写外层死循环 用于判断输出的数据: 分析: 最外层for,来控制最外层行数,存储 ...