简体   繁体   English

使用 handleClick 从 React 中的卡片组件获取道具

[英]Using handleClick to get props from a card component in React

I have three Components -> A Game component which renders a GameBoard component, which renders various BeerCard Components.我有三个组件-> 一个游戏组件,它呈现一个 GameBoard 组件,它呈现各种 BeerCard 组件。

The BeerCards Components are just Cards - that render from data passed as props via the GameBoard, which comes via an api call to a server and stores in the state. BeerCards 组件只是卡片 - 从通过 GameBoard 作为道具传递的数据进行渲染,这些数据通过 api 调用到服务器并存储在 state 中。

I'm trying to access the props of the individual card {name, brewery, ID} via a click function passed down from the Game component, this then adds the props to the state in the Game component, which will be then used to submit the selected cards back as a POST to the server.我正在尝试通过单击从 Game 组件传递下来的 function 访问单个卡 {name, brewery, ID} 的道具,然后将道具添加到 Game 组件中的 state 中,然后用于提交选择的卡片作为 POST 返回到服务器。 But can't fathom how to access those?但无法理解如何访问这些? Any ideas?有任何想法吗?

class GameBoard extends Component {
    constructor(props) {
        super(props);
        this.state = { beers: [], error: null };
    }
    componentDidMount() {
        axios.get('/api/game').then(
            result => {
                this.setState({ beers: result.data.beer });
            },
            error => {
                this.setState({ error });
            }
        );
    }

    render() {
        const beerList = this.state.beers.map(beer => (
            <BeerCard
                name={beer.name}
                brewery={beer.brewery}
                photo={beer.photoURL}
        key={beer._id}
        id={beer._id}
        onClick={this.props.onClick}
            />
        ));
        return <div className="row">{beerList}</div>;
    }
}

BeerCard Component啤酒卡组件

const BeerCard = ({ name, brewery, photo, onClick }) => (
    <div className="col-12 col-md-6 col-xl-4">
        <div className="card" style={{width: '20rem', height: '20rem'}} onClick={onClick}>
            <img className="card-img-top" src={photo} alt={name}></img>
            <div className="card-body">
                <h3 className="card-title">{name}</h3>
                <h5 className="card-text">{brewery}</h5>
            </div>
        </div>
    </div>
);

Game Component游戏组件


class Game extends Component {
    constructor(props) {
    super(props);
    this.state = { entry: []}
    this.handleClick = this.handleClick.bind(this, props)
    }
handleClick(props) {
  console.log(this.props)
}
    render() {
        return (
      <div>
         <GameBoard
          onClick={this.handleClick}
         /> 
      </div>

    )
    }
}
export default Game;

I'd use a context.我会使用上下文。 Because the Game component is where the data is going to be arranged and populated then to post, right?因为游戏组件是数据将被安排和填充然后发布的地方,对吗?

I don't know if it is the best option, but with a context you could write, and access data from every level of your app without having to pass it through props.我不知道这是否是最好的选择,但是您可以编写上下文,并从应用程序的各个级别访问数据,而无需通过道具传递数据。

I'd create all the logic of the api on the game component (or the most upper level component), and make it the provider of the context.我将在游戏组件(或最上层组件)上创建 api 的所有逻辑,并使其成为上下文的提供者。 So every child component can access to the functions.因此每个子组件都可以访问这些功能。

Update us!更新我们!

in GameBoard class change onClick={this.props.onClick} to onClick={() => this.props.onClick(bear)}GameBoard class 中将onClick={this.props.onClick}更改为onClick={() => this.props.onClick(bear)}
and in Game class make these changes:并在Game class 中进行以下更改:

class Game extends Component {
    constructor(props) {
    super(props);
    this.state = { entry: []}
    this.handleClick = this.handleClick.bind(this)
    }
handleClick(bear) {
  console.log(bear)
}
    render() {
        return (
      <div>
         <GameBoard
          onClick={this.handleClick}
         /> 
      </div>

    )
    }
}
export default Game;

I suggest you use redux to mange the states and actions我建议你使用redux来管理状态和动作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM