![](/img/trans.png)
[英]How do I pass a Prop to a Navigation Screen Component - React Native
[英]How do I pass a const variable to a component as a prop in React?
我正在尝试将 WantedAirline 变量传递给 AirlineSection 组件,但我不断收到错误TypeError: Cannot read property 'wantedAirline' of undefined
from the AirlineSection 组件。
render() {
const wantedAirline = this.state.wantedAirline.filter(p =>
p.name.includes(this.state.searchTerm)
)
return (
<div>
<header>Pokemon Searcher</header>
<AirlineSection
wantedAirline = {wantedAirline}
airlines = {this.state.airlines}
handleDelete={this.handleDelete}/>
</div>
)
}
AirlineSection 组件如下所示:
function AirlineSection(props){
console.log(props, 'airline section props');
return(
<div>
<h2>Airlines</h2>
{props.airlines.map(airline => <AirlineCard key={airline.id} wantedAirline={this.wantedAirline} airline={airline} handleDelete={props.handleDelete}/>)}
</div>
)
}
将wantedAirline={this.wantedAirline}
替换为wantedAirline={props.wantedAirline}
看起来AirlineSection
是您的功能组件。
您将wantedAirline
作为道具传递,因此您还必须使用道具访问它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.