Created
March 6, 2018 03:19
-
-
Save thai-ng/745f6640b22eea831e46b6620634c66b to your computer and use it in GitHub Desktop.
template typescript
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 * as React from 'react'; | |
import './App.css'; | |
const baseURL = 'http://www.sfu.ca/bin/wcm/course-outlines'; | |
interface MyProps {} | |
interface Data { | |
text: string; | |
value: number; | |
} | |
interface MyState { | |
years: Data[]; | |
error: boolean; | |
} | |
class Api extends React.Component<MyProps, MyState> { | |
constructor(props: MyProps) { | |
super(props); | |
this.state = { years: [], error: false }; | |
this.revealResults = this.revealResults.bind(this); | |
} | |
componentDidMount() { | |
fetch(baseURL) | |
.then(response => { | |
if (response.ok) { | |
return response.json(); | |
} else { | |
throw new Error('Something went wrong ...'); | |
} | |
}) | |
.then(data => { | |
this.setState({ years: data }); | |
}) | |
.catch(error => this.setState({error})); | |
} | |
getYears() { | |
return baseURL; | |
} | |
revealResults() { | |
// return <p>{this.state.years[0]}</p>; | |
global.console.log('hello ' + this.state.years[0].value); | |
} | |
render() { | |
return ( | |
<div className="Api"> | |
<button onClick={this.revealResults}>Reveal</button> | |
</div> | |
); | |
} | |
} | |
export default Api; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment