【发布时间】:2018-04-12 22:23:19
【问题描述】:
我在尝试访问我的道具中的数据时一直遇到此错误。
我正在使用 @connect 装饰器将状态映射到道具,如下所示:
@connect((store) => {
return {
listAdProductsData: store.listAdProductsData.data,
foxFooterData: store.foxFooterData.data
}
})
但是,当我记录 this.props 时,我得到了三个 props 实例。其中两个在加载时未填充,第三个最终填充了数据。
我该如何解决这个问题?到目前为止,我发现的唯一方法是做一个“愚蠢”的检查,看看数据是否是一个空的对象数组,如下所示:
getData(prop) {
if (prop.data === undefined) {
return [{}];
}
return prop.data;
}
我在 componentWillMount 中还有一个调度函数来获取数据 - 这样做有什么问题吗? :
componentWillMount() {
//Fetch Ad Products Data
this.props.dispatch(fetchAdProductsData())
//Fetch List Ad Products Data
this.props.dispatch(fetchListAdProductData())
//Fetch Fox Footer Data
this.props.dispatch(fetchFoxFooterData());
}
这是我的组件代码:
import React from 'react';
import HeroVideo from '../../../assets/videos/HERO_Landing.mp4';
import styles from './styles.css';
export default class HeroModule extends React.Component {
render() {
let data = this.props.data[0];
console.log("PROPS", data);
console.log("Get data", data.productData[0].adProductsHeading);
return (
<div className="hero-wrap col-xs-12 no-padding">
<div className="video-container">
<video width="100%" height="auto" autoPlay loop muted className="fillWidth">
<source src={HeroVideo} type='video/webm' />
<source src={HeroVideo} type='video/mp4' />
Your browser does not support HTML5 video. Please upgrade your browser!
</video>
</div>
<div className="hero">
<div className="hero-header-wrap">
<div className="hero-header">
<h1></h1>
<h2>The big picture?</h2>
<button className="btn">Watch Video</button>
</div>
</div>
</div>
</div>
)
}
}
【问题讨论】:
-
你可以为你的组件添加完整的代码吗?会有帮助的
-
@AbdellahAlaoui 查看更新
-
@yorah 这是不同的 - 这是在填充后查询数据。
标签: json reactjs react-redux axios