From a098f79c275cedc99126b349e2719b6856d3c0be Mon Sep 17 00:00:00 2001 From: notnull Date: Sat, 22 Jun 2019 07:28:32 -0400 Subject: [PATCH] added error handling for data fetching --- src/App.js | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/App.js b/src/App.js index 164e9ef..3d2e230 100644 --- a/src/App.js +++ b/src/App.js @@ -35,24 +35,29 @@ class App extends React.Component { } async fetchTasks() { - console.log('Fetching tasks...') - const { data } = await axios.get(api + '/api/tasks') - await this.setState({ tasks: data }) + try { + const { data } = await axios.get('/api/tasks') + return data + } catch (error) { + console.log(error) + this.setState({ error }) + } } async fetchProjects() { - console.log('Fetching projects...') - const { data } = await axios.get(api + '/api/projects') - await this.setState({ projects: data }) + try { + const { data } = await axios.get('/api/projects') + return data + } catch (error) { + console.log(error) + this.setState({ error }) + } } async fetchData() { - await this.fetchTasks() - await this.fetchProjects() - await this.setState({ loading: false }) - await console.log( - 'Ok, everything should be loaded now and nothing should render after this without input!' - ) + const tasks = await this.fetchTasks() + const projects = await this.fetchProjects() + await this.setState({ loading: false, tasks, projects }) } componentDidMount() {