Skip to content

Instantly share code, notes, and snippets.

@amandeepmittal
Created December 6, 2018 13:28
Show Gist options
  • Save amandeepmittal/31cfe0fa5bead8e43870a345b03e694e to your computer and use it in GitHub Desktop.
Save amandeepmittal/31cfe0fa5bead8e43870a345b03e694e to your computer and use it in GitHub Desktop.
import fetch from 'isomorphic-fetch';
import Link from 'next/link';
import Error from 'next/error';
import Layout from '../components/Layout';
import StoryList from '../components/StoryList';
class Index extends React.Component {
static async getInitialProps({ req, res, query }) {
let stories;
let page;
try {
page = Number(query.page) || 1;
const response = await fetch(
`https://node-hnapi.herokuapp.com/news?page=${page}`
);
stories = await response.json();
} catch (err) {
console.log(err);
// Return Empty Stories when there is an error
stories = [];
}
return { page, stories };
}
render() {
const { stories, page } = this.props;
if (stories.length === 0) {
return <Error statusCode={503} />;
}
return (
<Layout title="Hacker News Clone">
<StoryList stories={stories} />
<button>
<Link href={`/?page=${page + 1}`}>
<a>Load More</a>
</Link>
</button>
<style jsx>
{`
button {
font-size: 24px;
font-weight: 600;
}
button a {
text-decoration: none;
}
`}
</style>
</Layout>
);
}
}
export default Index;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment