user2675966
user2675966

Reputation: 37

How to get data from child in an array from JSON file in React

I have this JSON file: Json file

I'm destructuring from the component and getting data from props:

class  CustomHeader extends Component {


    render() { 

        const { headerBackground, logo} = this.props.customdesign;
        console.log(logo.sizelogo);

    }


}

I have no problem when getting data from parent:

console.log(logo);

Console gives me: sizelogo: "12", urllogo: "http://fire.info"

but I can't get child data from logo in this case sizelogo:

console.log(logo.sizelogo);

Console gives me: TypeError: Cannot read property 'sizelogo' of undefined

I have try different things like:

class  CustomHeader extends Component {


    render() { 

        const { headerBackground, logo, logo: sizelogo} = this.props.customdesign;
        console.log(sizelogo);

    }


}

But just getting errors

Any idea how to get child data?

Upvotes: 0

Views: 247

Answers (1)

morissetcl
morissetcl

Reputation: 564

I guess you iterate over multiple Json object. Sometimes it works because you have a logo sometimes not. So either you make sur that your props is always fill or you can prevent the error.

To avoid to crash an error you can add this kind of statement (before your console logo)

const sizeLogo = this.logo ? this.logo.sizeLogo : "no logo"

Then

console.log(sizeLogo)

Upvotes: 1

Related Questions