2

trying to return json result to a component, trying to setup the json fetch in the separate file so i can reuse it. but stuggling

index.js

export const getData = (data) => {

return fetch('http://localhost:9968/api/vehicle')
.then(response => response.json())
.then((data) => {
    return response.json()
})

carlist.js

import React, { Component } from 'react';
import { getData } from '../api';

export default
class CarList extends Component {

constructor(props) {
    super(props);

    this.state = {
        data: null
    }
}

componentDidMount() {
    getData((data) => {
        this.setState({
            data
        })
    });
}

render() {
    if(this.state.data) {
        console.log(this.state.data);
        return (
            <h1>Car list</h1>
        )
    }

    return (<h1>Loading...</h1>);
}

}

this is the json i expect to print to screen, in postman i know the end point works but react is not returning anything

{
    "vehicles": [
        {
            "id": "x",
            "modelYear": "98",
            "url": "/api/vehicle/tt",
            "media": [
                {
                    "name": "vehicle",
                    "url": "/images/1.jpg"
                }
            ]
        },
        {
            "id": "z",
            "modelYear": "99",
            "url": "/api/vehicle/ff",
            "media": [
                {
                    "name": "vehicle",
                    "url": "/images/2.jpg"
                }
            ]
        },

    ]
   }
2
  • You pass a callback into getData in componentDidMount but you don't use it. Commented Sep 18, 2019 at 2:44
  • Hi, check my solution and let me know if that helps. Commented Sep 18, 2019 at 8:30

2 Answers 2

2

This function you are using returns promise which should return json response.So remove the response.json() only return response from promise.

export const getData = () => {
 return fetch('http://localhost:9968/api/vehicle')
  .then(response => response.json())
  .then((response) => {
   return response
})

Then use async and await for componentDidMount and also remove the data that you are passing to getData function.

async componentDidMount() {
let response = await getData();
this.setState({
  data: response
  })
}
Sign up to request clarification or add additional context in comments.

Comments

1

Your getData function returns promise.

In CarList component you can do this,

componentDidMount() {
    getData(data).then((data) => {
        this.setState({
            data
        })
    });
}

Also your getData function is a bit wrong, you are returning response.json() instead of data, you should directly return data like,

export const getData = (data) => {
   return fetch('http://localhost:9968/api/vehicle')
    .then(response => response.json())
    .then((data) => {
      return data  //instead of `response.json()` return data directly
    })
}

Note: You are passing data to getData function but not using it, pass the data only when you need it.

1 Comment

@user2286483, have you tried this, is this helpful? Is it working?

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.