0

I am new to react andtrying to fetch JSON data in React JS but getting this error:

TypeError: Cannot read property 'data' of null

My code is :

import React from 'react';

export default class FetchJson extends React.Component {

    componentDidMount()
    {
        fetch('https://api.myjson.com/bins/9i63i')
        .then((response) => response.json())
        .then((findresponse) =>{
            this.setState({ data: findresponse })
            //console.log(this.state.data);
            //console.log(findresponse.DesignName);

        })
    }

    render() {
        return(
            <ul>
                {this.state.data.map((x,i) => <li key={i}>{x.DesignName}</li>)}
            </ul>
        );
    }
}

You can see the json data here: http://myjson.com/9i63i I want to retrieve value for key DesignName which is part1 which is not happening. See the commented lines: both gives me the value. But when i try to access it inside return method inside render. I get error : TypeError: Cannot read property 'data' of null in this line:

{this.state.data.map((x,i) => <li key={i}>{x.DesignName}</li>)}

How to solve this?

1
  • 1
    Probably because render() runs before you're .then() the callback has set the state (as this happens asynchronously). Check that this.state is defined before you try and map it: {this.state && this.state.data.map((x,i)...} Commented Jan 16, 2020 at 8:16

2 Answers 2

2

DesignName is not an array in the response.

You can define your state like this:

 state = {
    data: null
  }

And display the DesignName using inline if with logical && operator to solve null problem.

  render() {   
    return (
      <div>
        DesignName: { this.state.data && this.state.data.DesignName}
      </div>
    );
  }

Codesandbox

Sign up to request clarification or add additional context in comments.

7 Comments

thanks, conditional rendering solved my problem and the data was retrieved by this.state.data.DesignName.
done. Also i need access nested data, if you see the json then inside ComponentInfo there are ComponentName, FkComponentCategory, PartNumber, Manufacturer, Power, ComponentLevelPassPercentage. How can I access those? I tried this.state.data.ComponentInfo.ComponentName But didn't worked
@AnishArya ComponentInfo is an array. You can access it by index this.state.data.ComponentInfo[0].ComponentName or if you want to display them you can use map.
that worked, how to access whole array and display?
@AnishArya try to use map, I will update codesandbox later.
|
2

You can use an isLoading flag while waiting for your api call to finish.

state = {
   data: null,
   isLoading:true
}

render() { 
  if(this.state.isLoading) { 
    return(<div>loading</div>); 
  }
  return(
   <ul>
    {this.state.data.map((x,i) => <li key={i}>{x.DesignName}</li>)}
   </ul>
  );

when your api call has finished, you can update the state like this:

this.setState({ data: findresponse, isLoading:false })

1 Comment

Welcome to Stack Overflow, I endorse this answer as a clean solution to the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.