简体   繁体   English

预计在箭头 function 的末尾使用 if 语句返回一个值

[英]Expected to return a value at the end of arrow function with if statement

I am having this code.我有这个代码。

{
  Object.keys(_.get(order, "customer", {})).map((key) => {
    if (key !== "defaultOption" && customer[key] !== "") {
      return (
        <option key={key} value={key}>
          {_.startCase(key)}
        </option>
      );
    }
  });
}

And having below warning并有以下警告

  Line 885:96:  Expected to return a value at the end of arrow function  array-callback-return

I know because I am using if statement inside the map function.我知道是因为我在map function 中使用了if语句。 So, How to get rid of the error?那么,如何摆脱错误呢?

return something after the if statement (even if it is an empty string).if语句之后return一些东西(即使它是一个空字符串)。

You might want to filter such values out of the array afterwards (or just filter the array first instead of using an if ).之后您可能希望从数组中filter这些值(或者只是先filter数组而不是使用if )。

What ESLint is telling you is that if the condition is false, your map callback doesn't return anything, so the array that map creates will have undefined there. ESLint 告诉您的是,如果条件为假,您的map回调不会返回任何内容,因此map创建的数组将在那里undefined That's often a mistake, rather than something you're doing intentionally.这通常是一个错误,而不是你故意做的事情。

Have the function return something whether the condition is true or false.无论条件是真还是假,让 function 返回一些东西。 For instance, you might return null if the condition is false, if you don't want React to render anything for that entry.例如,如果条件为假,如果您不希望 React 为该条目呈现任何内容,您可能会返回null

But instead, I'd probably filter , then map :但相反,我可能会filter ,然后是map

{
  Object.keys(_.get(order, "customer", {}))
  .filter((key) => key !== "defaultOption" && customer[key] !== "")
  .map((key) => (
      <option key={key} value={key}>
        {_.startCase(key)}
      </option>
    )
  )
}

Side note: I've removed the ;旁注:我已经删除了; after map(/*...*/) in the above because it looks like it's inside a JSX expression.在上面的map(/*...*/)之后,因为它看起来像是在 JSX 表达式中。 ; is a statement terminator, which you can't have inside a JSX expression.是一个语句终止符,你不能在 JSX 表达式中使用它。 (Of course, if that isn't a JSX expression, ignore that change, though that would mean the code was creating option s that it never used... :-) ) (当然,如果这不是 JSX 表达式,请忽略该更改,尽管这意味着代码正在创建它从未使用过的option ... :-))

The root cause of the problem is due to return statement being inside an if condition .问题的根本原因是由于return 语句位于 if 条件内

This means that the function will return only when the condition is true.这意味着 function 只有在条件为真时才会返回。

The behaviour for condition being false also must be defined in order to resolve the issue.为了解决问题,还必须定义条件为假的行为。

This can be achieved in 3 ways:这可以通过 3 种方式实现:

  • Add a filter to obtain only the interesting values in the map (Recommended)添加过滤器以仅获取 map 中的有趣值(推荐)

  • Add an else block to return something when condition fails添加一个else 块以在条件失败时返回某些内容

  • or by adding an explicit return at the final line of the code block.或者通过在代码块的最后一行添加显式返回。

Here is an example of the suggested change:以下是建议更改的示例:

Option 1: Add a filter to get a map of interesting values only (recommended)选项 1:添加过滤器以仅获取有趣值的 map(推荐)

Object.keys(_.get(order, "customer", {}))
    .filter( 
        (interestingValue) => (
            interestingValue !== "defaultOption" && 
            customer[interestingValue] !== ""
        ) 
    )
    .map( 
        (key) => (
            <option key={key} value={key}>
                {_.startCase(key)}
            </option>
        )
    )

Option 2: Add an else block选项 2:添加 else 块

Object.keys(_.get(order, "customer", {}))
    .map( (key) => {
            if (key !== "defaultOption" && customer[key] !== "") {
                return (
                    <option key={key} value={key}>
                        {_.startCase(key)}
                    </option>
                )
            } else {
                return (
                    <div>condition failed</div>
                )
            }            
        }
    )

Option 3: Add an explicit return at the end of the block选项 3:在块末尾添加显式返回

Object.keys(_.get(order, "customer", {}))
    .map( (key) => {
            if (key !== "defaultOption" && customer[key] !== "") {
                return (
                    <option key={key} value={key}>
                        {_.startCase(key)}
                    </option>
                )
            }
            return undefined
        }
    )

You have to return something for map() to work with.必须返回一些东西供map()使用。 You cannot 'skip' values with map() .您不能使用map() '跳过'值。

Try returning null and then applying filter to filter out those null values afterwards.尝试返回null然后应用filter器过滤掉那些 null 值。

Just add return null after if statement.只需在 if 语句后添加 return null 即可。

Issue is there is no else block , what if condition got false问题是没有else block ,如果条件为false怎么办

map function expects some value in returns map function 期望回报有一些价值

{
  Object.keys(_.get(order, "customer", {})).map((key) => {
    if (key !== "defaultOption" && customer[key] !== "") {
      return (
        <option key={key} value={key}>
          {_.startCase(key)}
        </option>
      );
    } else { 
        return null;  // <----- You were missing this
    }
  });
}

OR或者

{
    Object.keys(_.get(order, "customer", {}))
    .filter((key) => (key !== "defaultOption" && customer[key] !== "") )
    .map((key) => (
        <option key={key} value={key}>
          {_.startCase(key)}
        </option>
      );
    );
}

You can convert this into a loop.您可以将其转换为循环。

{
    (()=>{
        let arr=[];
        const keys = Object.keys(_.get(order, "customer", {}));
        for(let key of keys){
            if (key !== "defaultOption" && customer[key] !== "") {
                arr.push(<option key={key} value={key}>
                    {_.startCase(key)}
                </option>)
            }
        }
        return arr;
    })()
}

You need to return value outside of your if statement within your map function.您需要在 map function 中的 if 语句之外返回值。 Since you are not doing that you are being warned by eslint.由于您没有这样做,因此您会受到 eslint 的警告。

{
  Object.keys(_.get(order, "customer", {})).map((key) => {
    if (key !== "defaultOption" && customer[key] !== "") {
      return (
        <option key={key} value={key}>
          {_.startCase(key)}
        </option>
      );
    }
    return null;  // return value here 
  });
}

Now you can return null from within map function when your condition doesn't match as react doesn't render null values现在,当您的条件与反应不匹配时,您可以从 map function 中return null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 预期在react中的箭头函数末尾返回一个值 - Expected to return a value at the end of arrow function in react 重构:期望在箭头函数的末尾返回一个值 - Refactor: Expected to return a value at the end of arrow function 错误预期在箭头函数的末尾返回一个值 - Error Expected to return a value at the end of arrow function 预期在箭头函数错误的末尾返回一个值 - Expected to return a value at the end of arrow function error JavaScript,预期在箭头函数的末尾返回一个值 - JavaScript, Expected to return a value at the end of arrow function 预期在箭头函数结束时返回一个值:array-callback-return - Expected to return a value at the end of arrow function: array-callback-return 预期在箭头函数的结尾处返回一个值。 (一致返回) - Expected to return a value at the end of arrow function. (consistent-return) 预期在箭头函数array-callback-return的末尾返回一个值 - Expected to return a value at the end of arrow function array-callback-return 预期在箭头函数的末尾返回一个值consistent-return - Expected to return a value at the end of arrow function consistent-return 预期在 reactjs 上的箭头函数末尾返回一个值如何修复此错误? - Expected to return a value at the end of arrow function on reactjs how to fix this error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM