解決React報錯Expected an assignment or function call and instead saw an expression
正文
當(dāng)我們忘記從函數(shù)中返回值時,會產(chǎn)生"Expected an assignment or function call and instead saw an expression"錯誤。為了解決該錯誤,確保顯式地使用return語句或使用箭頭函數(shù)隱式返回。

下面有兩個示例來展示錯誤是如何產(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組件中,錯誤是在Array.map()方法中引起的。這里的問題在于,我們沒有從傳遞給map()方法的回調(diào)函數(shù)中返回任意值。
在JavaScript函數(shù)中,如果我們沒有顯式地使用return語句,或者使用箭頭函數(shù)隱式地返回一個值,則返回undefined。
mapStateToProps函數(shù)中的問題是一樣的,我們忘記從函數(shù)中返回值。
顯式返回
為了解決該錯誤,我們必須顯式地使用return語句或使用箭頭函數(shù)隱式返回值。
下面是一個例子,用來說明如何使用顯式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方法返回一個數(shù)組,其中包含我們傳遞給它的回調(diào)函數(shù)所返回的所有值。
需要注意的是,當(dāng)你從一個嵌套函數(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ù)返回。
需要注意的是,我們根本沒有使用大括號。簡短的隱式返回使用圓括號。
返回對象
如果我們使用隱式返回來返回一個對象,我們必須用圓括號來包裹這個對象。
// ? 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']
};
一個簡單的思考方式是--當(dāng)你使用大括號而沒有用圓括號包裹它們時,你是在聲明一個代碼塊(比如if語句)。
{
console.log('this is my block of code');
}
當(dāng)不使用圓括號時,你有一個代碼塊,而不是一個對象。
但當(dāng)你用圓括號包裹住大括號時,你就有一個隱式的箭頭函數(shù)返回。
如果你認(rèn)為eslint規(guī)則不應(yīng)該在你的方案中造成錯誤,你可以通過使用注釋來關(guān)閉某一行的eslint規(guī)則。
// eslint-disable-next-line no-unused-expressions
注釋應(yīng)該放在造成錯誤的那一行的正上方。
原文鏈接:bobbyhadz.com/blog/react-…
以上就是解決React報錯Expected an assignment or function call and instead saw an expression的詳細(xì)內(nèi)容,更多關(guān)于React報錯assignment function的資料請關(guān)注腳本之家其它相關(guān)文章!
- node.js使用express-jwt報錯:expressJWT?is?not?a?function解決
- React報錯信息之Expected?an?assignment?or?function?call?and?instead?saw?an?expression
- MySQL運行報錯:“Expression?#1?of?SELECT?list?is?not?in?GROUP?BY?clause?and?contains?nonaggre”解決方法
- 解決三元運算符 報錯“SyntaxError: can''''t assign to conditional expression”
- 解決大于5.7版本mysql的分組報錯Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated
- Express無法通過req.body獲取請求傳遞的數(shù)據(jù)解決方法
- express框架,報錯:“Cannot set headers after they are sent to the client”,解決方法總結(jié)
相關(guān)文章
React?antd中setFieldsValu的簡便使用示例代碼
form.setFieldsValue是antd?Form組件中的一個方法,用于動態(tài)設(shè)置表單字段的值,它接受一個對象作為參數(shù),對象的鍵是表單字段的名稱,值是要設(shè)置的字段值,這篇文章主要介紹了React?antd中setFieldsValu的簡便使用,需要的朋友可以參考下2023-08-08
React項目打包發(fā)布到Tomcat頁面空白問題及解決
這篇文章主要介紹了React項目打包發(fā)布到Tomcat頁面空白問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
解決React報錯The?tag?is?unrecognized?in?this?browser
這篇文章主要為大家介紹了解決React報錯The?tag?is?unrecognized?in?this?browser示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
React動畫實現(xiàn)方案Framer Motion讓頁面自己動起來
這篇文章主要為大家介紹了React動畫實現(xiàn)方案Framer Motion讓頁面自己動起來,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
react-native聊天室|RN版聊天App仿微信實例|RN仿微信界面
這篇文章主要介紹了react-native聊天室|RN版聊天App仿微信實例|RN仿微信界面,需要的朋友可以參考下2019-11-11
基于React和antd實現(xiàn)自定義進(jìn)度條的示例代碼
在現(xiàn)代 Web 開發(fā)中,進(jìn)度條是一種常見且實用的 UI 組件,用于直觀地向用戶展示任務(wù)的完成進(jìn)度,本文將詳細(xì)介紹如何使用 React 來構(gòu)建一個自定義的進(jìn)度條,它不僅能夠動態(tài)展示進(jìn)度,還具備額外的信息提示功能,需要的朋友可以參考下2025-03-03

