Last active
May 12, 2018 05:15
-
-
Save maisonm/7075fc721ee2b6baa951c2f6179de887 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import logo from './logo.svg'; | |
import './App.css'; | |
class App extends Component { | |
state = { | |
data: null | |
}; | |
componentDidMount() { | |
// Call our fetch function below once the component mounts | |
this.callBackendAPI() | |
.then(res => this.setState({ data: res.express })) | |
.catch(err => console.log(err)); | |
} | |
// Fetches our GET route from the Express server. (Note the route we are fetching matches the GET route from server.js | |
callBackendAPI = async () => { | |
const response = await fetch('/express_backend'); | |
const body = await response.json(); | |
if (response.status !== 200) { | |
throw Error(body.message) | |
} | |
return body; | |
}; | |
render() { | |
return ( | |
<div className="App"> | |
<header className="App-header"> | |
<img src={logo} className="App-logo" alt="logo" /> | |
<h1 className="App-title">Welcome to React</h1> | |
</header> | |
// Render the newly fetched data inside of this.state.data | |
<p className="App-intro">{this.state.data}</p> | |
</div> | |
); | |
} | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment