简体   繁体   English

反应-TypeError:无法读取未定义的属性'imageurl'

[英]React - TypeError: Cannot read property 'imageurl' of undefined

I looked all over the internet and still couldn't find the right answer. 我在互联网上四处张望,但仍然找不到正确的答案。 I get this error 'TypeError: Cannot read property 'imageurl' of undefined'. 我收到此错误“ TypeError:无法读取未定义的属性'imageurl'”。 React is rendering first before componentDidMount or anything else I have tried. React是在componentDidMount或其他我尝试过的东西之前先渲染的。

This is my component: 这是我的组件:

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { fetchProductImageBackground } from '../../Redux/actions';

class ProductCategoryPage extends Component {

    componentDidMount() {
        fetchProductImageBackground();
    }


    render() {
        return (
            <div className="productCategoryPage" style={{backgroundImage: `url(${this.props.backgroundImages[0].imageurl})`}}>
                <div className="productCategoryPage__section">
                    <p className="productCategoryPage__section-type">product category</p>
                    <h1 className="productCategoryPage__section-title">category</h1>
                </div>
                <div className="productCategoryPage">

                </div>
            </div>
        );
    }
}

const mapStateToProps = state => {
    return {
        backgroundImages:state.productCategoryBackgroundImage
    }
}

export default connect(mapStateToProps, null)(ProductCategoryPage);

Just do 做就是了

if (!this.props.backgroundImages) return null;

at the beginning of your render() method. 在render()方法的开头。 Once the data is in place, your component gets rendered again. 数据放置到位后,组件将再次呈现。

How does fetchProductImageBackground() look like? fetchProductImageBackground()外观如何? In case it's synchronous, you could also call it during Redux store initialization. 如果它是同步的,您还可以在Redux存储初始化期间调用它。 Otherwise, you'd need to handle the "we're waiting for data" case anyway. 否则,您仍然需要处理“我们正在等待数据”的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM