简体   繁体   English

如何在 React.js 中使用 onClick() 事件将道具从一个组件传递到另一个组件

[英]How to pass props from one Component to another component with onClick() event in React.js

Quick Help Needed!需要快速帮助! I have Two React components Vendors and VendorsList .我有两个 React 组件VendorsVendorsList In Vendors.js Component i have asset.asset_name text rendered in table format.Vendors.js组件中,我将asset.asset_name文本以表格格式呈现。 What I want is, When I click on I asset.asset_name , I wanted to pass it's value from Vendors component to VendorsList component.我想要的是,当我点击 I asset.asset_name时,我想将它的值从Vendors组件传递给VendorsList组件。 How could I do this?我怎么能这样做?

Here is code for Two Components这是两个组件的代码

Vendors.js供应商.js

import React, { useEffect, useState } from "react";
import { axios } from "axios";

const Vendors = () => {
  const [data, setData] = useState({});
  const baseURL =
    "http://127.0.0.1:8000/api/business_process/business-impact/bussiness-assets-detail";

  useEffect(() => {
    axios
      .get(baseURL)
      .then((response) => {
        setData(response.data);
      })
      .then(
        (response) => {},
        (err) => {
          alert("No Data To Show");
        }
      )
      .catch((err) => {
        return false;
      });
  }, []);
  const DisplayData = data.business_assets?.map((asset) => {
    return (
      <tr>
        <td>{asset.business_assets}</td>
        <td onClick={() => alert(asset.asset_name)}>{asset.asset_name}</td>
      </tr>
    );
  });
  return <div></div>;
};

export default Vendors;

Here is VendorsList.js这是VendorsList.js

    import React from "react";
const VendorsList = () => {
  return (
    <div>
      <h1>{foo}</h1>
    </div>
  );
};

export default VendorsList;

I need asset.asset_name value to be passed to VendorsList when I click on asset.asset_name value from Vendors component当我单击Vendors组件中的asset.asset_name值时,我需要将asset.asset_name值传递给VendorsList

I don't think you're passing the asset_name into your VendorsList component at all.我认为您根本没有将asset_name传递到您的VendorsList组件中。 I think the quickest way is to directly render the VendorsList in your Vendors component by putting it in the return of your Vendors component.我认为最快的方法是直接在Vendors组件中呈现VendorsList ,方法是将其放入Vendors组件的返回中。 You would also need something to record what you have clicked so you can use another useState for this.您还需要一些东西来记录您单击的内容,以便您可以为此使用另一个useState Below is how you'd achieve this:以下是您如何实现这一目标:

Modify your Vendor.js to look like this:将您的Vendor.js修改为如下所示:

 import React, { useEffect, useState } from "react"; import { axios } from "axios"; import VendorsList from '<path to your VendorList component>' const Vendors = () => { const [data, setData] = useState({}); const [clickedAsset, setClickedAsset] = useState() const baseURL = "http://127.0.0.1:8000/api/business_process/business-impact/bussiness-assets-detail"; useEffect(() => { axios.get(baseURL).then((response) => { setData(response.data); }).then( (response) => {}, (err) => { alert("No Data To Show"); } ).catch((err) => { return false; }); }, []); const DisplayData = data.business_assets?.map((asset) => { return ( <tr> <td>{asset.business_assets}</td> <td onClick={() => setClickedAsset(asset.asset_name)}>{asset.asset_name}</td> </tr> ); }); return ( <div> <DisplayData/> <VendorList clickedAssetName={clickedAsset}/> </div> ); }; export default Vendors;

Then to use the clickedAssetName that you just passed, access it like this in your VendorsList.js component:然后要使用您刚刚传递的clickedAssetName ,请在您的VendorsList.js组件中像这样访问它:

 import React from "react"; const VendorsList = ({clickedAssetName}) => { return ( <div> <h1>{clickedAssetName}</h1> </div> ); }; export default VendorsList;

暂无
暂无

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

相关问题 如何在 React JS 中将 props onClick 从一个组件传递到另一个组件 - How to pass props onClick from one component to another in React JS 如何在 React.JS 的 onClick 事件侦听器中将道具发送到组件? - How to send props to a component in onClick event listener in React.JS? 如何在 React.JS 中将数据从一个组件传递到另一个组件 - How to pass data from one component to another in React.JS 在 React.js 中如何将表单的值从一个组件传递到另一个组件 - In React.js How to pass values of form from one component to another component 如何在 React.js 中将道具从一个类传递到另一个类 - How to pass props from one class to another in React.js 如何将由 OnClick 事件触发的异步函数中的道具传递给组件 React js - How to pass props from async functions triggered by an OnClick event to a component React js 在 React.js 中将 props 传递给父组件 - Pass props to parent component in React.js 如何在 React.js 中将 useState 值从一个组件传递到另一个组件 - How to pass useState Value from One Component to another one in React.js 如何通过道具[REACT.JS]将图片网址从父组件传递到子组件 - How can I pass image url from parent component to child component via props [REACT.JS] 如何使用 react.js 中的编程路由将道具从子组件(addform)传递到主应用程序组件 - How to pass props from child component (addform) to main app component using programmatic routing in react.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM