Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

394
Views
fetch() retrieved data in JS (React) throws error with no apparent reason

I think is related to babel or javascript-language package in the text editor (Atom), because I am actually coding along a tutorial and I am just starting and I already get error after running "npm start or yarn start". I have exactly the same code as in the tutorial but in my case it prompts the following errors:

enter image description here

This is my code:

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

class App extends Component {
  constructor(){
    super()
    this.state= {
      monsters: []
    };
  }
  componentDidMount(){
    fetch('https://jsonplaceholder.typicode.com/users')
    .then(response => response.json())
    .then(data =>
      this.setState(
        ()=>{
          monsters: data
        }
      )
    );
  }
  render(){
    return <div className='App'>
    <h1>{this.state.monsters}</h1>
    <ul>{this.state.monsters.map((monster)=>{
      return <li>{monster.name}</li>;
    })}</ul></div>;
  }
}

export default App;

I hope someone can give me a hint on what should I do or try, in order to fix this...

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The curly braces here make this a function, and it's expecting a return value. Your intention is to return an object. To do that, add parentheses.

// From this:
()=>{
  monsters: data
}

// To this:
()=> ({
  monsters: data
})

That should do the trick.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!