【发布时间】:2021-04-04 14:21:53
【问题描述】:
我是新来的反应。我有一个产品页面组件,它是显示特定产品的页面。我想要做的是这个 ProductPage 在 products 中找到特定的产品(这是一个存储为 props 的对象数组)并将其保存为状态,然后再渲染出来。
我收到了这些错误。我无法从数组中过滤掉单个产品。
1) currentProduct is not defined.
2)Error: Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops.
我应该使用哪种生命周期方法来解决这个问题?我很迷惑。谁能给我解释一下?
在我的 ProductPage.js 中,
import React, { Component } from 'react';
import { connect } from 'react-redux';
class ProductPage extends Component {
constructor(props) {
super(props)
this.state = {
product: [],
productId: this.props.match.params._id,
}
this.productsFiltering = this.productsFiltering.bind(this);
}
// update the state so that a product can be rendered
componentDidUpdate() {
const currentProduct = this.productsFiltering(this.state.productId);
this.setState({
product: currentProduct,
})
}
// find a product based on the Id
// pass the _id from url to find out the single product
productsFiltering = (productId) => {
const productArray = this.props.products;
return productArray.find(product => product._id === productId)
}
render() {
// error
const {name, price, description} = product;
return (
<div>
<span>{product.name}</span>
<span>{product.price}</span>
<span>{product.description}</span>
</div>
)
}
}
const mapStateToProps = (state) => ({
products: state.productsContainer,
});
export default connect(
mapStateToProps,
)(ProductPage);
【问题讨论】:
-
我认为你在
productsFiltering中有错字 - 看起来应该是productArray.find(product => product._id === productId)。 -
根本问题是您正在将道具复制到状态,而无需这样做。
ProductPage显示产品并且不会更改,因此不需要状态。只需抓取匹配的产品并将其存储在变量中然后渲染。不需要setState()。 -
@MrCode 感谢您的回答。它有很大帮助。我按照你刚才所说的,终于弄明白了!
-
@sam1024 很高兴我帮助解决了这个问题!我添加了一个答案,以便您接受并完成问题(帮助人们查看哪些问题仍需要答案)。
标签: reactjs