当我们忘记从函数中返回值时,会产生"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'; //相关文章
- 解决React报错JSX element type does not have
- 解决React报错Unexpected default export of anonymou
- 解决React报错Expected `onClick` listener to be
- 解决React报错Encountered two children with the
- 解决React报错Functions are not valid as a React ch
- React中的Hooks路由跳转问题
- 解决React报错Cannot assign to 'current' because it
- 解决React报错The tag is unrecognized in this
- 解决React报错Style prop value must be an object
- vue3中如何使用ref和reactive定义和修改响应式数据(最
- c# DataView.ToTable()方法 去除表的重复项问题