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)該放在造成錯誤的那一行的正上方。
到此這篇關(guān)于React報錯之Expected an assignment or function call and instead saw an expression的文章就介紹到這了,更多相關(guān)React報錯內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(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?Native?加載H5頁面的實現(xiàn)方法
這篇文章主要介紹了React?Native?加載H5頁面的實現(xiàn)方法,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04
使用React+ts實現(xiàn)無縫滾動的走馬燈詳細(xì)過程
這篇文章主要給大家介紹了關(guān)于使用React+ts實現(xiàn)無縫滾動的走馬燈詳細(xì)過程,文中給出了詳細(xì)的代碼示例以及圖文教程,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2023-08-08
redux持久化之redux-persist結(jié)合immutable使用問題
這篇文章主要為大家介紹了redux持久化之redux-persist結(jié)合immutable使用問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
React Native全面屏狀態(tài)欄和底部導(dǎo)航欄適配教程詳細(xì)講解
最近在寫 React Native 項目,調(diào)試應(yīng)用時發(fā)現(xiàn)頂部狀態(tài)欄和底部全面屏手勢指示條區(qū)域不是透明的,看起來很難受。研究了一下這個問題,現(xiàn)在總結(jié)一下解決方案,這篇文章主要介紹了React Native全面屏狀態(tài)欄和底部導(dǎo)航欄適配教程2023-01-01
react中的useImperativeHandle()和forwardRef()用法
這篇文章主要介紹了react中的useImperativeHandle()和forwardRef()用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08

