Skip to content

Instantly share code, notes, and snippets.

@koss-lebedev koss-lebedev/Posts.tsx
Last active May 20, 2019

Embed
What would you like to do?
import React from 'react'
import fetch from 'isomorphic-fetch'
import { NextFunctionComponent } from 'next'
type Props = PromiseResult<ReturnType<typeof getInitialProps>>
const Posts: NextFunctionComponent<Props> = ({ 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 () => {
const subreddit = 'typescript'
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
You can’t perform that action at this time.