[英]React Passing Data From Child Component to Parent Component via a onClick
I am new to react and am currently working on a react rails application. 我是新来的React,目前正在研究React Rails应用程序。
<h1><img src={activity.image_url}/></h1>
<p> {activity.name} </p>
<p> Rating: {activity.rating} </p>
<p><img src={activity.rating_img_url}/></p>
<p>{activity.location.display_address.join(" ")}</p>
<p><a href={activity.url}>Learn More</a></p>
<input id="add_to" className="btn" type="submit" onClick={props.addto} value="Add to Profile"/>
Currently, this I am using props.data.map((activity) => ...
to map over the data. My question is are there any ways I could somehow pass in the .name, .rating, .url etc.
upon clicking the "Add to Profile"
button (and store it into a variable for my parent component to use)? I have been looking all over for an answer but due to my lack of experience with react, I am struggling to find an answer I can understand. 目前,我正在使用
props.data.map((activity) => ...
来映射数据。我的问题是我可以通过任何方式将.name, .rating, .url etc.
传递给单击"Add to Profile"
按钮(并将其存储到供我的父组件使用的变量中)吗?我一直在寻找答案,但是由于缺乏响应的经验,我一直在努力寻找答案可以理解。
I have also put a debugger in my top level component and it hits my debugger when I click the button "Add to Profile 我还在顶级组件中放置了一个调试器,当我单击“添加到配置文件”按钮时,它将击中调试器。
handleAddTo(event) {
debugger;
event.preventDefault();
}
I really would appreciate any help on this! 我真的很感谢任何帮助! Thank you for your time.
感谢您的时间。
由于您正在使用道具中的addTo
函数,因此我假设这是来自正确的父组件,因此您可以做的就是将值绑定到该函数,例如
<input id="add_to" className="btn" type="submit" onClick={props.addto.bind(this, activity.name, activity.rating, activity.rating_img_url)} value="Add to Profile"/>
You can just pass values you want in parents like 您可以像父母一样传递您想要的值
<input id="add_to" className="btn" type="submit" onClick={props.addto(activity.name, activity.rating, activity.rating_img_url)} value="Add to Profile"/>
and use it 并使用它
handleAddTo(name, rating, imgUrl) {
// do something with values
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.