[英]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.