Created
May 20, 2019 14:54
-
-
Save koss-lebedev/68f0c4c64e1383de7c9ead08fedc6b50 to your computer and use it in GitHub Desktop.
This file contains 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 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