0

I'm attempting to consume a JSON API using ; the error mentioned above appears on the following line:

this.state.data.map( (dynamicData,key)=> 

This is my ReactJS code with the error line in bold:

import React, { Component } from 'react';
import './App.css';

class App extends Component {

//constructor
constructor() {
  super();
  this.state = {
    data: [],
  }
} //end constructor

componentDidMount(){
  return fetch('https://jsonplaceholder.typicode.com/todos')
  .then((response)=>response.json())
  .then((responseJson)=>
{
  this.setState({
    data:responseJson.todos
  })
  console.log(this.state.data)
})
} // end component did mount

  render() {
    return (
      <div>
      <h2>Todo:</h2>
          <div>
          {
            **this.state.data.map( (dynamicData,key)=>**
            <div>
                <span> {dynamicData.userId} </span>
                <span> {dynamicData.id} </span>
            </div>
            )
          }
          </div>
      </div>
    );
  }
}

export default App;

Could I get some help as to what I'm doing wrong?

3
  • what does responseJson.todos look like? Commented Apr 26, 2018 at 2:50
  • this is the url to the json am attempting to consume: jsonplaceholder.typicode.com/todos Commented Apr 26, 2018 at 2:53
  • hmm, you don't want to return your fetch() but not sure if that is the problem Commented Apr 26, 2018 at 2:56

1 Answer 1

1
import React, { Component } from "react";
import { render } from "react-dom";

class App extends Component {
  state = {
    data:[],
    url: "https://jsonplaceholder.typicode.com/todos"
  };

  componentDidMount() {
    fetch(this.state.url)
      .then(response => response.json())
      .then(data => this.setState({ data }));
  }

  render() {
    const { data } = this.state;
    data && console.log(data);
    return (
      <div>
        {data &&
          data.map(item => <div> Hello User With Id: {item.userId} </div>)}
      </div>
    );
  }
}

render(<App />, document.getElementById("root"));

Your didMount should look like mine also, setState takes a callback so if you wanted to see what the data looked like it would be like this

this.setState({ data }, () => console.log(this.state.data))

Edit Fetch Testing robinhood top 10

In your render it looks like you forgot the parenthesis after the arrow function in map.

render() {
    return (
      <div>
      <h2>Todo:</h2>
          <div>
          {
            this.state.data.map((dynamicData,key)=> (
            <div>
                <span> {dynamicData.userId} </span>
                <span> {dynamicData.id} </span>
            </div>
            ))
          }
          </div>
      </div>
    );
  }
Sign up to request clarification or add additional context in comments.

2 Comments

Omar, ...thank you! The data now load, one quick question, I notice all of the data load excepts for completed e.g item.completed ...is there some modification required in order for the completed to load too? ...thanks again
yes check my code sandbox <div> {" "} Hello User With Id: {item.userId} {item.completed ? ( <div> Completed </div> ) : ( <div> In Progress </div> )}{" "} </div>

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.