Skip to content

Instantly share code, notes, and snippets.

View koss-lebedev's full-sized avatar

Konstantin koss-lebedev

View GitHub Profile
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 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
}
}
type Props = ReturnType<typeof getInitialProps>
/*
type Props = Promise<{
subreddit: string;
posts: readonly RedditPost[];
}>
*/
declare type PromiseResult<T> = T extends Promise<infer U> ? U : T
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>
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
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>
type NextFunctionComponent<Props = {}, InitialProps = Props, Context = NextContext>
const INCREMENT_BY = 'counter/INCREMENT_BY'
const DECREMENT_BY = 'counter/DECREMENT_BY'
const incrementBy = (num) => ({
type: INCREMENT_BY,
payload: num
})
const decrementBy = (num) => ({
type: DECREMENT_BY,
payload: num
import * as React from "react"
import { Quote } from "./canvas"
export function KanyeQuote() {
const [quote, setQuote] = React.useState("")
React.useEffect(() => {
fetch("https://api.kanye.rest/").then(response => {
response.json().then(json => {
setQuote(json.quote)