Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.