解決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
語(yǔ)句或使用箭頭函數(shù)隱式返回。
下面有兩個(gè)示例來(lái)展示錯(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()
方法中引起的。這里的問(wèn)題在于,我們沒(méi)有從傳遞給map()
方法的回調(diào)函數(shù)中返回任意值。
在JavaScript函數(shù)中,如果我們沒(méi)有顯式地使用return
語(yǔ)句,或者使用箭頭函數(shù)隱式地返回一個(gè)值,則返回undefined
。
mapStateToProps
函數(shù)中的問(wèn)題是一樣的,我們忘記從函數(shù)中返回值。
顯式返回
為了解決該錯(cuò)誤,我們必須顯式地使用return
語(yǔ)句或使用箭頭函數(shù)隱式返回值。
下面是一個(gè)例子,用來(lái)說(shuō)明如何使用顯式return
來(lái)解決這個(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;
我們通過(guò)在map()
方法中顯式返回來(lái)解決問(wèn)題。這是必須的,因?yàn)?code>Array.map方法返回一個(gè)數(shù)組,其中包含我們傳遞給它的回調(diào)函數(shù)所返回的所有值。
需要注意的是,當(dāng)你從一個(gè)嵌套函數(shù)中返回時(shí),你并沒(méi)有同時(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ù)返回。
需要注意的是,我們根本沒(méi)有使用大括號(hào)。簡(jiǎn)短的隱式返回使用圓括號(hào)。
返回對(duì)象
如果我們使用隱式返回來(lái)返回一個(gè)對(duì)象,我們必須用圓括號(hào)來(lái)包裹這個(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)而沒(méi)有用圓括號(hào)包裹它們時(shí),你是在聲明一個(gè)代碼塊(比如if
語(yǔ)句)。
{ 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ò)使用注釋來(lái)關(guān)閉某一行的eslint
規(guī)則。
// eslint-disable-next-line no-unused-expressions
注釋應(yīng)該放在造成錯(cuò)誤的那一行的正上方。
原文鏈接:bobbyhadz.com/blog/react-…
以上就是解決React報(bào)錯(cuò)Expected an assignment or function call and instead saw an expression的詳細(xì)內(nèi)容,更多關(guān)于React報(bào)錯(cuò)assignment function的資料請(qǐng)關(guān)注腳本之家其它相關(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無(wú)法通過(guò)req.body獲取請(qǐng)求傳遞的數(shù)據(jù)解決方法
- express框架,報(bào)錯(cuò):“Cannot set headers after they are sent to the client”,解決方法總結(jié)
相關(guān)文章
React實(shí)現(xiàn)浮層組件的思路與方法詳解
React?浮層組件(也稱為彈出組件或彈窗組件)通常是指在用戶界面上浮動(dòng)顯示的組件,本文主要介紹了浮層組件的實(shí)現(xiàn)方法,感興趣的小伙伴可以了解下2024-02-02react component changing uncontrolled in
這篇文章主要為大家介紹了react component changing uncontrolled input報(bào)錯(cuò)解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12詳解create-react-app 自定義 eslint 配置
這篇文章主要介紹了詳解create-react-app 自定義 eslint 配置,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-06-06React進(jìn)階學(xué)習(xí)之組件的解耦之道
這篇文章主要給大家介紹了關(guān)于React進(jìn)階之組件的解耦之道,文中通過(guò)詳細(xì)的示例代碼給大家介紹了組件分割與解耦的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08react中的useImperativeHandle()和forwardRef()用法
這篇文章主要介紹了react中的useImperativeHandle()和forwardRef()用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08解決React報(bào)錯(cuò)The?tag?is?unrecognized?in?this?browser
這篇文章主要為大家介紹了解決React報(bào)錯(cuò)The?tag?is?unrecognized?in?this?browser示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12react如何使用useRef模仿抖音標(biāo)題里面添加標(biāo)簽內(nèi)容
本文介紹了如何模仿抖音發(fā)布頁(yè)面,使用div元素作為輸入框,并利用CSS樣式和JavaScript動(dòng)態(tài)操作DOM,實(shí)現(xiàn)類似于input輸入框的功能,感興趣的朋友跟隨小編一起看看吧2024-10-10解決react中l(wèi)abel標(biāo)簽for報(bào)錯(cuò)問(wèn)題
這篇文章主要介紹了react中l(wèi)abel標(biāo)簽for報(bào)錯(cuò)問(wèn)題,解決辦法就是react中l(wèi)abel標(biāo)簽沒(méi)有for屬性,用htmlFor代替for屬性,感興趣的朋友跟隨小編一起看看吧2022-02-02