1

I'm trying to replicate same code here with different JSON, but the data is not loading.

Please help, I'm not sure what is missing in the code.

import React from 'react';

export default class ItemLister extends React.Component {
    constructor() {
        super();
        this.state = { items: [] };
    }

    componentDidMount() {
  fetch('http://media.astropublications.com.my/api/drebar_landing.json')
      .then(result=>result.json())
      .then(items=>this.setState({items}));
    }

    render() {
      return(
        <ul>
            {this.state.items.length ?
              this.state.items.map(item=><li key={item.id}>{item.Title}</li>) 
              : <li>Loading...</li>
            }
        </ul>
     )
    }
  }
Azeem
  • 11,148
  • 4
  • 27
  • 40
Jepf
  • 83
  • 8

1 Answers1

2

Your api response contains an object ArticleObject and the ArticleObject has array of objects so you need to set the items.ArticleObject to the state.

Take a look at below solution for better understanding

componentDidMount() {
  fetch('http://media.astropublications.com.my/api/drebar_landing.json')
      .then(result=>result.json())
      .then(items=>this.setState({items:items.ArticleObject}));
    }
Hemadri Dasari
  • 32,666
  • 37
  • 119
  • 162
CodeZombie
  • 2,027
  • 3
  • 16
  • 30