reactjs - 无法从 map 和 forEach 函数中遍历 props 数据
问题描述
当我记录产品时,我能够在控制台窗口中获取数据,但无法从array
对象“产品”中遍历数据。我已经尝试过map函数和forEach函数。数据通过 props 来自 redux 状态,并通过componentWillReceiveProps
我调用setState
函数的位置保存到组件状态中。
请帮助解决此问题。欢迎任何建议!
此外,当在代码中添加调试器断点时,我无法在控制台窗口中获取数据,但是当我从代码中删除调试器断点时,我能够在控制台窗口中看到数据。
import React from 'react';
import {connect} from 'react-redux';
import {fetchProduct} from '../actions/UploadAction';
import ProductCrudList from './ProductCrudList'
class ProductCrud extends React.Component {
constructor(props){
super(props);
this.state={
products:[],
x:1,
test:[{value:1},{value:2}]
}
}
componentWillMount = () =>{
this.props.fetchProduct();
}
componentWillReceiveProps=(nextProps)=>{
if(this.props!=nextProps){
this.setState((state)=>({products: nextProps.products}));
}
}
render(){
// debugger
const {products, test, x} = this.state;
console.log(products);
products.forEach(p=>{
console.log(p);
})
return (
<div>
<div className="">
<h2 style={{textAlign:'center', margin:'20px'}}>PRODUCTS</h2>
{
}
{/* <ProductCrudList products={products} /> */}
</div>
{/* <h1>Working</h1> */}
</div>
)
}
}
const mapStateToProps = (state) => {
console.log(state);
return {
products: state.products.product
}
}
const mapDispatchToProps = (dispatch, ownProps) => {
return {
fetchProduct: () => {
dispatch(fetchProduct())
}
}
}
export default connect(mapStateToProps, mapDispatchToProps)(ProductCrud);
添加了一张图片,您可以在其中看到显示对象数组的控制台窗口
解决方案
推荐阅读
- bash - 如何过滤来自 speedtest-cli Linux 的 URL
- excel - 使用 Excel 宏打开任何文件
- arrays - 旋转阵列中最长的子阵列
- uppaal - 无限延迟但没有正率
- spring-boot - 错误:使用spring boot mongodb时未定义方法where(String)
- c# - 统一鼓掌旋转子对象
- networking - Oracle 云虚拟机上的 Kubernetes + Calico
- android - 来自 Android 应用程序的 Web 服务调用需要 10 分钟才能反映最新结果
- tcl - 如何通过 TCL 访问 ModelSim 中的 VHDL 信号属性?
- sharepoint - 分配后 SharePoint 发送通知电子邮件