[英]Return statement leads to: Expected an assignment or function call and instead saw an expression no-unused-expressions
I sometimes still have a hard time understanding when or when not to use a return statement.我有时仍然很难理解何时或何时不使用return语句。 I recently ran into an issue in my ReactJS code.我最近在我的 ReactJS 代码中遇到了一个问题。 Basically I get html from a fetch call then set it as the innerHTML of an element.基本上我从 fetch 调用中得到 html 然后将其设置为元素的 innerHTML。 I need to change that data a bit after setting it.设置后我需要稍微更改该数据。
When it doesn't work:当它不起作用时:
useEffect(()=>{
setHeaderFooter(props.data.header,props.data.footer, changeLinks)
}, [props.data.header,props.data.footer])
const changeLinks = ()=>{
$(document).find('nav.navbar a[href*="/oc"]').each(function (index, element) {
var original = $(element).attr('href');
var final = process.env.REACT_APP_PLATFORM_URL + original;
$(element).attr('href', final);
});
// Adds the first letter of the users name as the icon in header.
$('#firstLetter').text()
$('#firstLetterLink').text().charAt(0).toUpperCase()
}
export const setHeaderFooter=(head,foot, callback) =>{
let header = document.querySelector('#header')
let footer = document.querySelector('#footer')
header.innerHTML = head, footer.innerHTML = foot
return callback()
}
When it works (Without the modification phase at the end):当它工作时(最后没有修改阶段):
useEffect(()=>{
setHeaderFooter(props.data.header,props.data.footer)
}, [props.data.header,props.data.footer])
const changeLinks = ()=>{
$(document).find('nav.navbar a[href*="/oc"]').each(function (index, element) {
var original = $(element).attr('href');
var final = process.env.REACT_APP_PLATFORM_URL + original;
$(element).attr('href', final);
});
// Adds the first letter of the users name as the icon in header.
$('#firstLetter').text()
$('#firstLetterLink').text().charAt(0).toUpperCase()
}
export const setHeaderFooter=(head,foot) =>{
let header = document.querySelector('#header')
let footer = document.querySelector('#footer')
return header.innerHTML = head, footer.innerHTML = foot
}
Can someone give me an explanation as to what is happening here.有人可以解释一下这里发生了什么。 I want to understand the return statement better.我想更好地理解 return 语句。
Problem is the comma used in-between header
and footer
assignment:问题是在header
和footer
分配之间使用的逗号:
Change this:改变这个:
header.innerHTML = head, footer.innerHTML = foot
return callback()
To this:对此:
header.innerHTML = head;
footer.innerHTML = foot;
return callback();
To explain the issue see the MDN docs :要解释这个问题,请参阅MDN 文档:
The comma operator (,) evaluates each of its operands (from left to right) and returns the value of the last operand逗号运算符 (,) 计算其每个操作数(从左到右)并返回最后一个操作数的值
In your case, the footer.innerHTML = foot
is the last operand and will be returned but since you're not assigning the returned value to anything, it goes wasted and hence the lint error you get.在您的情况下, footer.innerHTML = foot
是最后一个操作数,将被返回,但由于您没有将返回的值分配给任何东西,所以它被浪费了,因此您得到了 lint 错误。 You can read more about the lint error at Disallow Unused Expressions (no-unused-expressions)您可以在Disallow Unused Expressions (no-unused-expressions)中阅读有关 lint 错误的更多信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.