简体   繁体   English

React:箭头函数需要返回值array-callback-return

[英]React: Arrow function expected a return value array-callback-return

I have the following snipper of code and I get an error Arrow function expected a return value array-callback-return but I can't find the problem out. 我有以下代码片段,我得到一个错误Arrow function expected a return value array-callback-return但我找不到问题。

  render() {
    return (
      <div>
        <Form addCharacter={this.addCharacter} name="Rick" />
        <Input
          onChange={this.filterCharacters}
          placeholder="Filters characters"
        />
        <Container>
          <Row>
            {this.state.characters.map((ch, i) => {
              if (ch.name.contains(this.state.filter_name)) {
                return (
                  <Col>
                    <Card
                      key={i}
                      img={ch.image}
                      rmCharacter={this.rmCharacter}
                      name={ch.name}
                      status={ch.status}
                      gender={ch.gender}
                      episode={this.extractEpisodes(ch.episode)}
                    />
                  </Col>
                );
              } else {
                return;
              }
            })}
          </Row>
        </Container>
      </div>
    );
  }

I set an else in the if to make sure I'm always returning something, but it doesn't seem to work. 我在if设置了一个else,以确保我总是返回一些东西,但它似乎不起作用。

Thanks a lot in advance! 非常感谢提前!

The problem appears to be with the map as you're trying to map items but when it goes to else, you're just doing a return. 当您尝试映射项目时,问题似乎与地图有关,但当它转到其他地方时,您只是在做回报。

Instead of an if else in the map function, I'll rather filter and then map on this.state.characters and remove the internal if else check. 我宁愿过滤然后映射this.state.characters并删除内部的if else检查,而不是map函数中的if else。

this.characters.filter(..).map(..);

you can do like this, 你可以这样做,

          {
            this.state.characters.map((ch, i) => ch.name.contains(this.state.filter_name) &&
                <Col>
                  <Card
                    key={i}
                    img={ch.image}
                    rmCharacter={this.rmCharacter}
                    name={ch.name}
                    status={ch.status}
                    gender={ch.gender}
                    episode={this.extractEpisodes(ch.episode)}
                  />
                </Col>
              )
         }

暂无
暂无

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

相关问题 预期在箭头函数array-callback-return中返回一个值 - Expected to return a value in arrow function array-callback-return 预期在箭头函数结束时返回一个值:array-callback-return - Expected to return a value at the end of arrow function: array-callback-return 预期在箭头函数array-callback-return的末尾返回一个值 - Expected to return a value at the end of arrow function array-callback-return 反应警告 预期在箭头函数数组-回调-返回结束时返回一个值 - react warning Expected to return a value at the end of arrow function array-callback-return 期望在过滤函数的箭头函数array-callback-return结尾处返回一个值 - Expected to return a value at the end of arrow function array-callback-return on filter function 正确警告:“预期在箭头函数数组回调返回中返回一个值” - Correct warning : "Expected to return a value in arrow function array-callback-return " 如何修复“预期在箭头函数数组回调返回中返回一个值”? - How to fix "Expected to return a value in arrow function array-callback-return"? 如何修复警告“预计在箭头 function 数组回调返回中返回一个值” - How fix warning “Expected to return a value in arrow function array-callback-return” 为什么我收到此警告 159:35 警告预计将在箭头 function 数组回调返回的末尾返回一个值? - Why i am getting this warning 159:35 warning Expected to return a value at the end of arrow function array-callback-return? Array.prototype.map() 期望来自箭头的返回值 function array-callback-return - Array.prototype.map() expects a return value from arrow function array-callback-return
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM