Skip to content
Advertisement

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. In Vendors.js Component i have asset.asset_name text rendered in table format. 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. How could I do this?

Here is code for Two Components

Vendors.js

JavaScript

Here is VendorsList.js

JavaScript

I need asset.asset_name value to be passed to VendorsList when I click on asset.asset_name value from Vendors component

Advertisement

Answer

I don’t think you’re passing the asset_name into your VendorsList component at all. 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. You would also need something to record what you have clicked so you can use another useState for this. Below is how you’d achieve this:

Modify your Vendor.js to look like this:

JavaScript

Then to use the clickedAssetName that you just passed, access it like this in your VendorsList.js component:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement