網站首頁 編程語言 正文
React報錯信息之Expected?an?assignment?or?function?call?and?instead?saw?an?expression_React
作者:chuckQu ? 更新時間: 2022-10-17 編程語言正文從這開始~
總覽
當我們忘記從函數中返回值時,會產生"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
注釋應該放在造成錯誤的那一行的正上方。
原文鏈接:https://www.cnblogs.com/chuckQu/p/16603535.html
相關推薦
- 2022-11-20 TS?中的類型推斷與放寬實例詳解_其它
- 2023-07-13 react中useState的基本用法
- 2022-09-02 Docker資源限制Cgroup的深入理解_docker
- 2023-04-21 numpy.insert()的具體使用方法_python
- 2022-04-19 Python的進程間通信詳解_python
- 2022-12-09 在DOS界面如何運行python的py文件_python
- 2021-11-13 Gateway網關工作原理及使用方法_其它綜合
- 2022-03-26 C++約瑟夫環問題詳解_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支