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 { NextContext } from 'next' | |
// we define our type | |
type Context = NextContext<{ subreddit: string }> | |
// ... | |
const getInitialProps = async (context: Context) => { | |
// now we can get subreddit value from the query! | |
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 | |
} | |
} | |
/* | |
* But we get an error here :( | |
* | |
* Type '(context: NextContext<{ subreddit: string; }, {}>) => | |
* Promise<{ subreddit: string; posts: readonly RedditPost[]; }>' is not assignable to type | |
* 'GetInitialProps<{ subreddit: string; posts: readonly RedditPost[]; }, | |
* NextContext<Record<string, string | string[] | undefined>, {}>>'. | |
*/ | |
Posts.getInitialProps = getInitialProps |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment