ReactJS - Cannot read property 'map' of undefined and not getting data

No worries, I will try this now, also trying to get a REPL up and running but it has issues installing axios!! Cheers

you can use fetch instead of axios tho. Both do the same thing

1 Like

Yeah I was going to try that but have learnt that axios is supposed to be better.

Anyway, so I did that and it loads, no data is show and then is does

Bear in mind that it came up with this before too!

Cheers!

hmm can you try console.log the response value?

Give me a min, I am trying to spin up a REPL and you can see my code. Thx for help :slight_smile:

okay.

this is the code that i made before if you need a reference

class App extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            posts: []
        };
    }

    componentDidMount() {
        fetch('https://jsonplaceholder.typicode.com/posts')
            .then(response => response.json())
            .then(json => {
                this.setState({
                    posts: json
                });
            });
    }

    render() {
        const { posts } = this.state;

        return (
            <div>
                <h1>List of posts!</h1>
                {posts.length > 0 ?
                    posts.map(post => {
                        return (
                            <div>{post.id} - {post.title}</div>
                        )
                    })
                    : (
                        <div>loading...</div>
                    )}
            </div>
        );
    }
}
1 Like

Thank you let me have a look and see what I can do with it! cheers :slight_smile:

1 Like