Skip to content

Instantly share code, notes, and snippets.

@maisonm
Last active May 12, 2018 05:15
Show Gist options
  • Save maisonm/7075fc721ee2b6baa951c2f6179de887 to your computer and use it in GitHub Desktop.
Save maisonm/7075fc721ee2b6baa951c2f6179de887 to your computer and use it in GitHub Desktop.
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