Last active
May 20, 2019 14:56
-
-
Save koss-lebedev/effad720c75f2879660334ddec1e9f46 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 } from 'next' | |
type Props = { | |
posts: readonly RedditPost[] | |
subreddit: string | |
} | |
// 1. We use NextFunctionComponent type here | |
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> | |
) | |
// 2. This no longer causes a type error | |
Posts.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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment