Skip to content

Instantly share code, notes, and snippets.

@johndavedecano
Last active October 17, 2018 15:17
Show Gist options
  • Save johndavedecano/94a0d020677bda5f0fb4ca21ba0f92e1 to your computer and use it in GitHub Desktop.
Save johndavedecano/94a0d020677bda5f0fb4ca21ba0f92e1 to your computer and use it in GitHub Desktop.
Handle multiple requests with reactJS
import React from 'react'
import axios from 'axios'
export default class extend React.Component {
state = {
profile: {},
memes: {},
isLoaded: false,
isLoading: false
}
componentDidMount() {
this.loadAll()
}
loadAll = async() => {
try {
this.setState({isLoading: true})
const request1 = axios.get('/users/1')
const request2 = axios.get('/users/1/memes')
const [profile, memes] = await Promise.all(request1, request2)
this.setState({
isLoading: false,
isLoaded: true,
profile,
memes
})
} catch(err) {
alert(err.message);
this.setState({isLoading: false})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment