[英]ESLint no-return-assign and no-param-reassign error in React code
I have this code in React App to load my chat messages:我在 React App 中有这段代码来加载我的聊天消息:
const chatBubbles = dummyData.map((obj, i = 0) => {
<div className={`${classes.bubbleContainer} ${obj.direction}`} key={i}>
<div key={(i += 1)} className={classes.bubble}>
<div className={classes.button}>{obj.message}</div>
</div>
</div>;
});
return <div className={classes.container}>{chatBubbles}</div>;
This is working, but I'm getting two errors in ESLint and I don't know how to fix it这是有效的,但我在 ESLint 中遇到两个错误,我不知道如何修复它
error Expected to return a value in arrow function array-callback-return
error Expected an assignment or function call and instead saw an expression no-unused-expressions
error Assignment to function parameter 'i' no-param-reassign
How can I rewrite this code snippet so that my Eslint will accept?如何重写此代码片段以便我的 Eslint 接受?
First issue: No reason to set a default value!第一个问题:没有理由设置默认值!
const chatBubbles = dummyData.map((obj, i = 0) => { <-- there should not be a `= 0`
Second issue: No return inside the block第二个问题:块内没有返回
const chatBubbles = dummyData.map((obj, i = 0) => { <-- there { should be a ( <div/> ) or { return (<div/>) }
Third issue: Incrementing index which is set by map第三期:由 map 设置的递增索引
(i += 1) <-- Why are you increasing the variable i?
const chatBubbles = dummyData.map((obj, i) => (
<div className={`${classes.bubbleContainer} ${obj.direction}`} key={i}>
<div key={(i + 1)} className={classes.bubble}>
<div className={classes.button}>{obj.message}</div>
</div>
</div>;
));
return <div className={classes.container}>{chatBubbles}</div>;
I am not sure why you are adding one to the second key...我不确定您为什么要向第二个键添加一个...
You don't need to pre-assign i
when using the map()
function on arrays.在 arrays 上使用
map()
function 时,您不需要预先分配i
。 Replace the first line with this:将第一行替换为:
const chatBubbles = dummyData.map((obj, i) => {
Maybe try to make it like this:也许试着让它像这样:
const chatBubbles = dummyData.map((obj, i) => {
return (
<div className={`${classes.bubbleContainer} ${obj.direction}`} key={i}>
<div key={(i + 1)} className={classes.bubble}>
<div className={classes.button}>{obj.message}</div>
</div>
</div>;
)
});
return <div className={classes.container}>{chatBubbles}</div>;
error Expected an assignment or function call and instead saw an expression
and error Expected to return a value in arrow function
- you need to return something from map callback error Expected an assignment or function call and instead saw an expression
和error Expected to return a value in arrow function
- 您需要从 map 返回值
error Assignment to function parameter 'i'
- you should not reassign i
( i += 1
change to i + 1
) error Assignment to function parameter 'i'
- 你不应该重新分配i
( i += 1
更改为i + 1
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.