简体   繁体   English

通过onClick反应将数据从子组件传递到父组件

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

相关问题 将数据从子级传递到父级组件-反应-通过回调函数 - passing data from child to parent component - react - via callback function 在本机反应中将数据从子组件传递到父组件? - Passing data from child component to parent component in react native? 通过点击从子组件到父组件反应传递数据 - Pass data via on click from child component to parent component react 将数据从父组件传递到子组件 - Passing data from parent component to a child component 将数据从子组件传递到父组件在反应中不起作用 - Passing data from child to parent component not working in react 将数据 onClick 从子组件传递到 React.JS 中的另一个子组件 - Passing data onClick from child component to another child component in React.JS 将数据从子组件传递到父组件/React 钩子 - Passing data from child to parent component / React hooks React,将状态从子组件传递给父组件 - React, passing state from a Child component to the Parent React:通过onClick将属性从功能组件传递到类组件 - React: Passing attribute from functional component to class component via onClick 在嵌套组件反应js中在子组件和父组件之间传递数据 - passing data between child and parent component in nested component react js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM