Skip to content

Instantly share code, notes, and snippets.

@amandeepmittal
Created December 6, 2018 13:28
Show Gist options
  • Save amandeepmittal/60b762dc26bd2d155a4cc3316ba09b01 to your computer and use it in GitHub Desktop.
Save amandeepmittal/60b762dc26bd2d155a4cc3316ba09b01 to your computer and use it in GitHub Desktop.
import fetch from 'isomorphic-fetch';
class Index extends React.Component {
static async getInitialProps() {
let stories;
try {
const response = await fetch(
'https://node-hnapi.herokuapp.com/news?page=1'
);
stories = await response.json();
} catch (err) {
console.log(err);
stories = [];
}
return { stories };
}
render() {
const { stories } = this.props;
return (
<div>
<h1>Hacker News Clone</h1>
<div>
{stories.map(story => (
<h3 key={story.id}>{story.title}</h3>
))}
</div>
</div>
);
}
}
export default Index;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment