正文从这开始~

总览

当我们忘记从函数中返回值时,会产生"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

注释应该放在造成错误的那一行的正上方。

最新文章

  1. C语言中struct位域的定义和使用
  2. 10款html5开发工具,实用+好用
  3. HTML中网页超链接设计
  4. Bellman-Ford
  5. 参考_Android中,如何新建一个界面,并且实现从当前界面切换到到刚才新建的(另外一个)界面
  6. jquery判断对象是否获得焦点
  7. android模拟器访问localhost或127.0.0.1报错
  8. gpu显存(全局内存)在使用时数据对齐的问题
  9. How to install / setup /upgrade PHP 5.5.x on Ubuntu 12.04 LTS
  10. 零件库管理信息系统设计--part03:管理员登录部分设计
  11. 二:熟悉 TCP/IP 协议
  12. java工具类(四)之实现日期任意跳转
  13. CentOs7 最小安装版安装后配置和java环境的搭建
  14. 复习C++:VS2008中的宏干嘛用的
  15. bzoj1594 Pku3764 The xor-longest Path
  16. spring boot学习(7) SpringBoot 之表单验证
  17. Windows Phone中解决多模块多程序集之间相互循环引用的问题一种思路
  18. Mkdir方法
  19. Depth of field --Circle of confusion 推导
  20. css美化、优化、合并工具推荐

热门文章

  1. 3000帧动画图解MySQL为什么需要binlog、redo log和undo log
  2. Redis集群搭建 三主三从
  3. python自动将新生成的报告作为附件发送并进行封装
  4. Fiddler对安卓高版本进行抓包解决方案以及分析 进阶二
  5. 运筹学笔记12 大M法
  6. 开源项目 PM 浅谈如何设计官网
  7. 记一次APP渗透登录验证绕过思路
  8. 《ECMAScript 6 入门》【三、字符串的扩展】(持续更新中……)
  9. oracle-安装与访问、卸载
  10. Navicat中查询mysql版本