简体   繁体   English

Return 语句导致:预期一个赋值或 function 调用,而是看到一个表达式 no-unused-expressions

[英]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:问题是在headerfooter分配之间使用的逗号:

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.

相关问题 得到预期赋值或函数调用的错误,而是看到一个表达式 no-unused-expressions? - Getting the error that Expected an assignment or function call and instead saw an expression no-unused-expressions? 得到预期的赋值或函数调用的错误,而是在反应中看到了一个表达式 no-unused-expressions - getting an error of Expected an assignment or function call and instead saw an expression no-unused-expressions in react 期望一个赋值或函数调用,而是在React中看到一个表达式no-unused-expressions错误 - Expected an assignment or function call and instead saw an expression no-unused-expressions error in React Object.keys-需要一个赋值或函数调用,而是看到一个表达式no-unused-expressions - Object.keys - Expected an assignment or function call and instead saw an expression no-unused-expressions 如何解决这个 期望赋值或函数调用,而是看到一个表达式 no-unused-expressions - how to solve this Expected an assignment or function call and instead saw an expression no-unused-expressions 重定向(反应路由器dom),给出了期望的赋值或函数调用,而是看到了一个表达式no-unused-expressions - Redirect (react router dom) giving Expected an assignment or function call and instead saw an expression no-unused-expressions “预期分配或 function 调用,而是在我的聊天应用程序中看到一个表达式 no-unused-expressions” - “Expected an assignment or function call and instead saw an expression no-unused-expressions” in my chat app 在React中导入文件时,获得期望的赋值或函数调用,而是看到一个表达式no-unused-expressions - Getting Expected an assignment or function call and instead saw an expression no-unused-expressions, when import a file in React 反应:期望一个赋值或函数调用,而是看到一个表达式 no-unused-expressions - React: expected an assignment or function call and instead saw an expression no-unused-expressions 期望一个赋值或函数调用,而是看到一个表达式no-unused-expressions(第14/15行) - Expected an assignment or function call and instead saw an expression no-unused-expressions (lines 14/15)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM