[英]How do I pass a const variable to a component as a prop in React?
I am trying to pass the wantedAirline variable into the AirlineSection Component but I keep getting the error TypeError: Cannot read property 'wantedAirline' of undefined
from inside the AirlineSection component.我正在尝试将 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>
)
}
The AirlineSection Component looks like this: 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>
)
}
Replace wantedAirline={this.wantedAirline}
with wantedAirline={props.wantedAirline}
as looks like AirlineSection
is your functional component.将
wantedAirline={this.wantedAirline}
替换为wantedAirline={props.wantedAirline}
看起来AirlineSection
是您的功能组件。
you are passing wantedAirline
as props so you also have to access it using props.您将
wantedAirline
作为道具传递,因此您还必须使用道具访问它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.