如何使用React组件作为函数?

时间:2019-05-17 14:12:31

标签: reactjs

是否可以传递参数以将组件作为函数进行响应,我有要在子组件中使用的父组件,我想将子组件的值传递给父组件进行渲染

export default class CardComponent extends React.Component {
  render() {
    return (
      <div>
        <button type="button"> {this.props}</button>
      </div>
    );
  }
}
import CardComponent from '../cardComponent/cardComponent'

export default class Events extends React.Component {
  render() {
    return (
      <div>
          <h1>Hello from events</h1>
          <CardComponent name="name" />
          <CardComponent name="location" />
          <CardComponent name="description" />
          ...
      </div>
    )
  }
}

1 个答案:

答案 0 :(得分:0)

CardComponent this.props中的错误应为this.props.name

export default class CardComponent extends React.Component {
  render() {
    return (
      <div>
        <button type="button"> {this.props.name}</button>
      </div>
    );
  }
}