简体   繁体   English

如何在 React 中将 const 变量作为道具传递给组件?

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

相关问题 如何将 Prop 传递给导航屏幕组件 - React Native - How do I pass a Prop to a Navigation Screen Component - React Native 如何在 React 和 Typescript 中将组件作为道具传递? - How do I pass a component as a prop in React and Typescript? 如何在父项更改时将道具传递给反应组件但不更新子项中的该道具? - How do I pass a prop to a react component yet not update that prop in the child when parent changes? 如何将React组件作为prop传递 - How to pass a React component as a prop 如何将组件作为道具传递给 React 中的另一个组件? - How can I pass a component as a prop into another component in React? React:如何将“未定义”变量作为道具传递给组件? - React: How can you pass a “not defined” variable as a prop to a component? 如何将 promise 中的值传递给 react native 的组件 prop? - How do I pass a value from a promise to a component prop in react native? React Reusable Button Component - 如何正确传递背景颜色道具? - React Reusable Button Component - How do I properly pass a background color prop? 如何为带有数据数组的 React 组件传递内联样式的道具? - How do I pass a prop for inline styling for React component with an array of data? 如何将 map 操作作为道具传递给组件(反应日期选择器)? - How can I pass a map operation to a component (react datepicker) as a prop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM