简体   繁体   English

重构:期望在箭头函数的末尾返回一个值

[英]Refactor: Expected to return a value at the end of arrow function

I have following code:我有以下代码:

<DetailsBox title={t('catalogPage.componentDetails.specs.used')}>
  {component?.projects.map(project => {
    projectList?.map(name => {
      if (project.id === name.id) {
        return (
          <Typography variant="body2" gutterBottom classes={{}}>
            {`${name.name}`}
          </Typography>
        );
      }
    });
  })}
</DetailsBox>

and I got Expected to return a value at the end of arrow function , I know that it is caused by return in anonymous function, but how to refactor it?我得到了Expected to return a value at the end of arrow function ,我知道它是由匿名函数中的return引起的,但是如何重构它?

Edit: The error is assigned to following line: projectList?.map(name => {编辑:错误分配给以下行: projectList?.map(name => {

The warning is because you're only sometimes returning something from the .map ;警告是因为你只是有时从.map返回一些东西; the resulting array will currently sometimes contain undefined elements interspersed with Typography components.生成的数组目前有时会包含散布着Typography组件的undefined元素。

Use .filter instead, to filter out the IDs that don't match:改用.filter来过滤掉不匹配的 ID:

<DetailsBox title={t('catalogPage.componentDetails.specs.used')}>
  {component?.projects.map(project => (
    projectList
      ?.filter(name => project.id === name.id)
      ?.map(name => (
          <Typography variant="body2" gutterBottom classes={{}}>
            {`${name.name}`}
          </Typography>
      ))
  ))}
</DetailsBox>

Or, if there happens to be only a single match at most for every pair, it would be more appropriate to create an object or Map of IDs to the projects items or to the projectList items - this will reduce the computational complexity to O(n) from O(n ^ 2) .或者,如果恰好是只有一个匹配最多每对,它会更合适创建ID的对象或映射到projects的项目或将projectList项目-这将计算复杂度降低O(n)来自O(n ^ 2)

Your parent function isn't returning a value.您的父函数没有返回值。 So, you either need to use parentheses for an implicit return or an explicit return statement:因此,您需要为隐式返回或显式返回语句使用括号:

Implicit:隐含:

<DetailsBox title={t('catalogPage.componentDetails.specs.used')}>
  {component?.projects.map(project => (
    projectList?.map(name => {
      if (project.id === name.id) {
        return (
          <Typography variant="body2" gutterBottom classes={{}}>
            {`${name.name}`}
          </Typography>
        );
      }
    ));
  })}
</DetailsBox>

Explicit return:显式返回:

<DetailsBox title={t('catalogPage.componentDetails.specs.used')}>
  {component?.projects.map(project => {
    return projectList?.map(name => {
        if (project.id === name.id) {
          return (
            <Typography variant="body2" gutterBottom classes={{}}>
              {`${name.name}`}
            </Typography>
          );
        }
    })
  })}
</DetailsBox>

Try尝试

<DetailsBox title={t('catalogPage.componentDetails.specs.used')}>
  {component?.projects.map(project => {
   return  projectList?.map(name => {
      if (project.id === name.id) {
        return (
          <Typography variant="body2" gutterBottom classes={{}}>
            {`${name.name}`}
          </Typography>
        );
      }
      return null
    });
    
  })}
</DetailsBox>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM