1

I'm taking axios to get a json server with an array

attached json array

http://my-json-server.typicode.com/jee4nc/myjsonserver/lista

The problem is that I want to assign that list of objects to another component through its properties

import React, { Component } from 'react';
import axios from 'axios';
import CardsGrid from "../Pages/CardsGrid"

class Axios_cards extends Component {

    constructor(props) {

        super(props)
        this.state = {
            courses : []
        }      
    }

    componentDidMount() {
        axios.get('http://my-json-server.typicode.com/jee4nc/myjsonserver/lista')
        .then(response => this.setState({
                courses: response.data
            }))
    }
    render() {
        const {courses} = this.state
        return <CardsGrid courses={courses}/
    }
}

export default Axios_cards;

Exactly here

render() {
        const {courses} = this.state
        return <CardsGrid courses={courses}/>
    }

The component that receives the list of objects as props, is not receiving the array, since I did a validation to verify it I attach the component to which I assign the array through props

const CardsGrid = ({courses}) => (
    <div className="ed-grid m-grid-3">
            {
                Array.isArray(courses) ?
            courses.map( e => 
                <Cards 
                id={e.id} 
                title={e.title} 
                description={e.description}
                image={e.image}
                price={e.price}
                key={e.id}  
                />) : null
            }
        </div>
)

3
  • 2
    What do you get if you add console.log(courses) in CardsGrid? (you will have to temporarily refactor it to use return for this). Commented Feb 9, 2020 at 22:24
  • 2
    You can also use the React Developer Tools (chrome.google.com/webstore/detail/react-developer-tools/…) to see the value of courses you are passing to CardsGrid. Commented Feb 9, 2020 at 22:26
  • Where could I find information on how to refactor a function component that receives props to a class component? I'm new to react :( Commented Feb 9, 2020 at 23:03

1 Answer 1

1

The issue is here axios.get('http://my-json-server.typicode.com/jee4nc/myjsonserver/lista'). Most probably you are opening your app over secure (https) connection (i.e. https://my-app). But in the code you're trying to get insecure (http) resource. This is called mixed content and is blocked by browsers. You can read more about this here

So to resolve, just use https endpoint. Your backend seems to support https requests.

axios.get('https://my-json-server.typicode.com/jee4nc/myjsonserver/lista')

Working sample

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

4 Comments

I just made the change, but still not receiving the list of objects using props. I've been learning javascript on my own, and I don't know how to see what CardsGrid is getting through props (courses)
To see what CardsGrid is getting thru props use either console.log(courses) and open dev console in a browser to see output (press F12 when in a browser and select Console tab) or change <Card> component to <div> or another standard HTML element like I did in my sample.
"Undefined" returns to me, it is strange considering that through axios it should be assigned correctly. In the axios_cards component I can't find the error that this undefined
Put console.log to .then(response => { console.log(response); this.setState({ courses: response.data })).to see if it ever called. It may be some error which should be sown in the console of a browser.

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.