React報(bào)錯(cuò)信息之Expected?an?assignment?or?function?call?and?instead?saw?an?expression
正文從這開始~
總覽
當(dāng)我們忘記從函數(shù)中返回值時(shí),會(huì)產(chǎn)生"Expected an assignment or function call and instead saw an expression"錯(cuò)誤。為了解決該錯(cuò)誤,確保顯式地使用return
語句或使用箭頭函數(shù)隱式返回。
下面有兩個(gè)示例來展示錯(cuò)誤是如何產(chǎn)生的。
// 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
組件中,錯(cuò)誤是在Array.map()
方法中引起的。這里的問題在于,我們沒有從傳遞給map()
方法的回調(diào)函數(shù)中返回任意值。
在JavaScript函數(shù)中,如果我們沒有顯式地使用return
語句,或者使用箭頭函數(shù)隱式地返回一個(gè)值,則返回undefined
。
mapStateToProps
函數(shù)中的問題是一樣的,我們忘記從函數(shù)中返回值。
顯式返回
為了解決該錯(cuò)誤,我們必須顯式地使用return
語句或使用箭頭函數(shù)隱式返回值。
下面是一個(gè)例子,用來說明如何使用顯式return
來解決這個(gè)錯(cuò)誤。
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()
方法中顯式返回來解決問題。這是必須的,因?yàn)?code>Array.map方法返回一個(gè)數(shù)組,其中包含我們傳遞給它的回調(diào)函數(shù)所返回的所有值。
需要注意的是,當(dāng)你從一個(gè)嵌套函數(shù)中返回時(shí),你并沒有同時(shí)從外層函數(shù)中返回。
隱式返回
另一種方法是使用箭頭函數(shù)的隱式返回。
// ??? 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;
我們?yōu)?code>App組件使用了隱式地箭頭函數(shù)返回。
需要注意的是,我們根本沒有使用大括號(hào)。簡(jiǎn)短的隱式返回使用圓括號(hào)。
返回對(duì)象
如果我們使用隱式返回來返回一個(gè)對(duì)象,我們必須用圓括號(hào)來包裹這個(gè)對(duì)象。
// ? 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'] };
一個(gè)簡(jiǎn)單的思考方式是--當(dāng)你使用大括號(hào)而沒有用圓括號(hào)包裹它們時(shí),你是在聲明一個(gè)代碼塊(比如if
語句)。
{ console.log('this is my block of code'); }
當(dāng)不使用圓括號(hào)時(shí),你有一個(gè)代碼塊,而不是一個(gè)對(duì)象。
但當(dāng)你用圓括號(hào)包裹住大括號(hào)時(shí),你就有一個(gè)隱式的箭頭函數(shù)返回。
如果你認(rèn)為eslint
規(guī)則不應(yīng)該在你的方案中造成錯(cuò)誤,你可以通過使用注釋來關(guān)閉某一行的eslint
規(guī)則。
// eslint-disable-next-line no-unused-expressions
注釋應(yīng)該放在造成錯(cuò)誤的那一行的正上方。
到此這篇關(guān)于React報(bào)錯(cuò)之Expected an assignment or function call and instead saw an expression的文章就介紹到這了,更多相關(guān)React報(bào)錯(cuò)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- node.js使用express-jwt報(bào)錯(cuò):expressJWT?is?not?a?function解決
- 解決React報(bào)錯(cuò)Expected an assignment or function call and instead saw an expression
- MySQL運(yùn)行報(bào)錯(cuò):“Expression?#1?of?SELECT?list?is?not?in?GROUP?BY?clause?and?contains?nonaggre”解決方法
- 解決三元運(yùn)算符 報(bào)錯(cuò)“SyntaxError: can''''t assign to conditional expression”
- 解決大于5.7版本mysql的分組報(bào)錯(cuò)Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated
- Express無法通過req.body獲取請(qǐng)求傳遞的數(shù)據(jù)解決方法
- express框架,報(bào)錯(cuò):“Cannot set headers after they are sent to the client”,解決方法總結(jié)
相關(guān)文章
React?Native?加載H5頁面的實(shí)現(xiàn)方法
這篇文章主要介紹了React?Native?加載H5頁面的實(shí)現(xiàn)方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04使用React+ts實(shí)現(xiàn)無縫滾動(dòng)的走馬燈詳細(xì)過程
這篇文章主要給大家介紹了關(guān)于使用React+ts實(shí)現(xiàn)無縫滾動(dòng)的走馬燈詳細(xì)過程,文中給出了詳細(xì)的代碼示例以及圖文教程,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-08-08redux持久化之redux-persist結(jié)合immutable使用問題
這篇文章主要為大家介紹了redux持久化之redux-persist結(jié)合immutable使用問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08React Hooks如何主動(dòng)更新Hooks組件
這篇文章主要介紹了React Hooks如何主動(dòng)更新Hooks組件問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11React Native全面屏狀態(tài)欄和底部導(dǎo)航欄適配教程詳細(xì)講解
最近在寫 React Native 項(xiàng)目,調(diào)試應(yīng)用時(shí)發(fā)現(xiàn)頂部狀態(tài)欄和底部全面屏手勢(shì)指示條區(qū)域不是透明的,看起來很難受。研究了一下這個(gè)問題,現(xiàn)在總結(jié)一下解決方案,這篇文章主要介紹了React Native全面屏狀態(tài)欄和底部導(dǎo)航欄適配教程2023-01-01react中的useImperativeHandle()和forwardRef()用法
這篇文章主要介紹了react中的useImperativeHandle()和forwardRef()用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08