Sign in
Log inSign up

Calling an API from render() method

Nirmalya Ghosh's photo
Nirmalya Ghosh
·Aug 22, 2017

Suppose, I've an API response for a blog like:

data: [{
    id: 1,
    attributes: {
        title: 'Hello world',
        description: 'Some description',
    },
    relationships: {
        author: {
            data: {
                id: 1,
                type: 'user',
            },
            links: {
                related: '/api/v1/users/1',
            }
        }
    }
}, { 
    ...
}]

Now, as you can see, I can render the blog title and description very easily. But, the data for the author has to be fetched while rendering.

I'm confused as how I can fetch the data for the author since I can't do the API call for the author from render() method. Also, since this is an array, I can do the API call from componentDidMount also!

Any help regarding how to handle this problem will be highly solicited. TIA.