【问题标题】:React Redux: Cannot read property '0' of undefinedReact Redux:无法读取未定义的属性“0”
【发布时间】: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());

    }

道具日志: 错误信息:

JSON 数据:

这是我的组件代码:

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


【解决方案1】:

我会从 getData() 中删除“愚蠢”的检查,并将这一行添加到 render() 的开头

if (!this.props.data ) { // or !this.props.data.length if your have an empty array initially
    return null; // or some loading indicator 
}

这样可以确保只有在成功填充数据时才会执行剩余的 render() 代码。

在您的商店中拥有一个 initialState 始终是一个好习惯。这将取代“愚蠢”检查的需要,而是使用具有空数组的初始数据填充存储。

【讨论】:

  • 我刚试过这个,很遗憾在这篇文章中得到了错误:无法读取未定义的属性'0'
  • 你能在收到错误之前 console.log(this.props) 吗?
  • 好的,我注意到了一个基本问题。我没有映射到道具的“数据”。 “数据”仅在诸如 foxFooterData 或 adProductsData 之类的映射道具中可用。我觉得我需要重组道具?
  • 不需要,但我的回答应该可以解决问题。确定我的代码放在了 render() 的开头吗?
【解决方案2】:

一种解决方案是使用 lodash _.get 安全地访问数据中深层嵌套的值。

console.log(_.get(this.props, 'data[0].productData[0].adProductsHeading'))

在获取数据之前,这将记录 undefined 而不是引发错误。

【讨论】:

  • 这正是我遇到的问题 - 在获取数据之前,我得到了未定义的日志。我试图阻止这种情况,以便组件仅在获取数据时呈现。
猜你喜欢
  • 2020-10-10
  • 2019-02-15
  • 1970-01-01
  • 2017-06-22
  • 2021-01-15
  • 2017-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多