HI,
here my main aim is to pass the id to other component during onclick.So i created a funcition call during onclick and that function should send props which is (pid) to Addtocart component and this Addtocart component should take the value and display onto console or to screen.
The problem is when i press onclick react is calling Addtoproducts and the id is also passed to Addtoproducts function but the id is not sent as props to Addtocart component
If there are any suggestions to improve this code please let me know
I am pasting my code with 2 files {showproduct.js,addtocart.js}
->showproduct.js
import React,{useState} from "react"
import ProductDetails from "./Data/ProductDetails.json"
import Addtocart from "./Addtocart"
const Addproducts=(id)=>{
console.log(id)
return <Addtocart pid="hello"/>
}
const Showproduct = ({match}) =>{
return(
<div>
{
ProductDetails.map(data =>{
if(data.id == match.params.id)
{
return(
<div>
<button className="btn btn-primary" onClick={() =>Addproducts(data.id)}>Add to cart</button>
</div>
)
}
}
}
</div>
)
}
export default Showproduct
->Addtocart.js
import React from "react";
const Addtocart =({pid}) =>
{
return(console.log(pid))
}
export default Addtocart
Thanks in advance