我在 React 电子商务网站上工作,但在显示 pdp 时遇到了我需要的所有字段(标题(确定)、价格(否)、描述(否)、图像(否))的困难。流程应该是,每当用户点击 <Picture Card />
组件时,它都应该打开该产品的 pdp。现在,如果发生这种情况,我设法通过 React Routes 在页面中显示标题,并且我可以正确注销我需要的所有字段,但是我无法让它出现在 pdp 页面上(我不知道如何传递数据在这里)。
我想将状态提升到我的父组件 (<App />
),然后将它作为道具传递给我的 <Pdp />
组件。但是我怎样才能这完全做到这一点?
现在,当我单击 <Picture Card />
组件时出现以下错误:“TypeError: this.props.parentCallback is not a function”
我错过了什么?提前致谢!
我的代码结构:
App.js
├── Header.js
├── Home.js
├── shop.js
├── PictureCard.js
├── Pdp.js
├── About.js
└── Footer.js
我的代码:
App.js
import React from "react"
import Header from "./components/Header"
import Main from "./components/Main"
import Footer from "./components/Footer"
import './App.css';
class App extends React.Component {
constructor() {
super()
this.state = {
data: null
}
}
handleCallback = (childData) => {
this.setState({
data: childData
})
}
render() {
return (
<div className="App">
<Header />
<Main parentCallback = {this.handleCallback}/>
<Footer />
</div>
)
}
}
export default App
Main.js
import React from 'react'
import { Switch, Route } from 'react-router-dom'
import Home from './Home'
import Shop from './Shop'
import About from './About'
import Pdp from './Pdp'
function Main() {
return (
<Switch> {/* The Switch decides which component to show based on the current URL.*/}
<Route exact path='/' component={Home}></Route>
<Route exact path='/shop' component={Shop}></Route>
<Route path='/pdp/:productTitle' component={Pdp}></Route>
<Route exact path='/about' component={About}></Route>
</Switch>
)
}
export default Main
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: "",
productTitle: "",
productPrice: "",
productDescription: "",
productImage: ""
}
this.handleClick = this.handleClick.bind(this)
}
handleClick(id, name, price, description, image) {
console.log(id, name, price, description, image)
this.props.parentCallback("Data from child") //this is wrong ? why ?
//getting the error TypeError: this.props.parentCallback is not a function
/* this.setState({
productId: id,
productTitle: name,
productPrice: price,
productDescription: description,
productImage: 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
Pdp.js
import React from "react"
import profile2 from "../images/profile2.jpg"
import { Link } from "react-router-dom"
import './Pdp.css'
class Pdp extends React.Component {
constructor() {
super()
}
render() {
return (
<div className="pdp-page">
<h3 className="filter-title">
<Link to="/shop" className="no-dec">All pictures</Link> <span>›</span> <a href="" className="no-dec">{this.props.match.params.productTitle}</a>
</h3>
<div className="pdp-container">
<div>
<img src={this.props.image} className="pdp-image"></img>
</div>
<div className="pdp-info-container">
<h3 className="pdp-title">{this.props.match.params.productTitle}</h3>
<p className="pdp-info-paragraph">€ price {this.props.price}</p>
<p className="pdp-info-paragraph">description {this.props.description}</p>
<button className="purchase-button">Purchase</button>
</div>
</div>
</div>
)
}
}
export default Pdp;
答案 0 :(得分:1)
试试这个
import React from 'react'
import { Switch, Route } from 'react-router-dom'
import Home from './Home'
import Shop from './Shop'
import About from './About'
import Pdp from './Pdp'
function Main({parentCallback}) {
return (
<Switch> {/* The Switch decides which component to show based on the current URL.*/}
//Please try this thing hope it will works
<Route exact path='/'
render={(props)=>
(<Home parentCallback={parentCallback}
{...props}/>
)}/>
<Route exact path='/shop'
render={(props)=>
(<Shop parentCallback={parentCallback}
{...props}/>
)}/>
<Route path='/pdp/:productTitle'
render={(props)=>
9<Pdp parentCallback={parentCallback}
{...props}/>
)}/>
<Route exact path='/about'
render={(props)=>
(<About parentCallback={parentCallback}
{...props}/>
)}/>
</Switch>
)
}
export default Main
答案 1 :(得分:0)
只要使用 redux,在任何地方传递数据都会容易得多。