Skip to content

Instantly share code, notes, and snippets.

@koss-lebedev
Created May 20, 2019 14:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save koss-lebedev/68f0c4c64e1383de7c9ead08fedc6b50 to your computer and use it in GitHub Desktop.
Save koss-lebedev/68f0c4c64e1383de7c9ead08fedc6b50 to your computer and use it in GitHub Desktop.
import React from 'react'
import fetch from 'isomorphic-fetch'
import { NextFunctionComponent, NextContext } from 'next'
type InitialProps = PromiseResult<ReturnType<typeof getInitialProps>>
type Props = InitialProps
type Context = NextContext<{ subreddit: string }>
const Posts: NextFunctionComponent<Props, InitialProps, Context> = ({ posts, subreddit }) => (
<div>
<h1>Posts in "{subreddit}"</h1>
<ul>
{posts.map(post => (
<li key={post.data.id}>{post.data.title}</li>
))}
</ul>
</div>
)
const getInitialProps = async (context: Context) => {
const subreddit = context.query.subreddit
const response = await fetch(`https://www.reddit.com/r/${subreddit}.json`)
const result = await response.json() as RedditResult
return {
subreddit,
posts: result.data.children
}
}
Posts.getInitialProps = getInitialProps
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment