gpt4 book ai didi

javascript - 查找 React 中单击的组件的值

转载 作者:行者123 更新时间:2023-12-01 02:07:55 27 4
gpt4 key购买 nike

我在 ReactJS 中有一个简单的 Component 类,如下所述。我想在单击按钮时获取名称字段的值。

var Movie = React.createClass({

handleBuy: function(){
console.log('tried adding to array');
cartItems.push({ id: 2, title: "Sholay", visible: false });
console.log('tried adding to array............');
},

render: function() {
return (
<tr>
<td>{this.props.movie.name}</td>
<td>{this.props.movie.day}</td>
<td>{this.props.movie.description}</td>
<td><button className="btn btn-info" onClick={this.handleBuy}>Buy Ticket</button></td>
</tr>);
}

});

我想打印单击按钮的行的 this.props.movi​​e.name 但我似乎不明白。

提前致谢。

最佳答案

使用一些参数调用 this.handleBuy ...

<button
className="btn btn-info"
onClick={() => this.handleBuy(<the data you care about>)}
>
Buy Ticket
</button>

关于javascript - 查找 React 中单击的组件的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49948447/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com