I am working on a React eCommerce website and I am having difficulties in showing the pdp (product detail page). The process should be that whenever a user clicks on a <Picture Card /> component, then it should open up the pdp of that product. Right now, if this happens I managed to log out the name and id of each product, but I cannot make it appear on the pdp page (I dont know how to pass the data here).
I cannot understand why setState() is not working correctly.
What am I missing ? Thanks in advance!
App.js
├── Header.js
├── Home.js
├── shop.js
├── PictureCard.js
├── Pdp.js
├── About.js
└── Footer.js
My code:
Shop.js
import React from "react"
import PictureCard from "./PictureCard"
import profile2 from "../images/profile2.jpg"
import { Link } from "react-router-dom"
import './Shop.css'
class Shop extends React.Component {
constructor() {
super()
this.state = {
productId: 1,
productTitle: "",
productPrice: "",
productDescription: "",
productImage: ""
}
this.handleClick = this.handleClick.bind(this)
}
handleClick(id, name, price, description, image) {
console.log(id) //the id here is correct
this.setState((prevState) => {
return {
productId: prevState.productId + 1
}
})
console.log(this.state.productId) //productId doen not update
}
//console.log(id, name, price, description, image)
render() {
return (
<div className="shop-container">
<h3 className="filter-title"><Link to="/shop" className="no-dec">All pictures</Link></h3>
<div className="shop-grid">
<PictureCard
id="1"
image={profile2}
title="Strandhill Cannon Susnet"
price="20"
description="Colourful sunset at the cannon of Strandhill during lockdown"
handleClick={this.handleClick}
/>
<PictureCard
id="2"
image={profile2}
title="Bundoran in Winter"
price="20"
description="Snowy mountains view behind Bundoran colourful houses"
handleClick={this.handleClick}
/>
<PictureCard
id="3"
image={profile2}
title="Mullaghmore Runner"
price="20"
description="Being active during lockdown in County Sligo"
handleClick={this.handleClick}
/>
</div>
</div>
)
}
}
export default Shop;
PictureCard.js
import React from "react"
import "./PictureCard.css"
import { Link } from "react-router-dom"
class PictureCard extends React.Component {
constructor() {
super()
}
render() {
return(
<div className="picure-card-container" onClick={() =>
this.props.handleClick(this.props.id, this.props.title, this.props.price, this.props.description, this.props.image)}>
<Link to = {`/pdp/${this.props.title}`} className="no-dec">
<img src={this.props.image} className="picture-card-image"></img>
<h6 className="picture-card-title">{this.props.title}</h6>
<p className="picture-card-price">€ {this.props.price}</p>
</Link>
</div>
)
}
}
export default PictureCard