Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Docs] Migrate with-context-api example to typescript #40297

Merged
merged 2 commits into from Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 0 additions & 31 deletions examples/with-context-api/components/Counter.js

This file was deleted.

57 changes: 57 additions & 0 deletions examples/with-context-api/components/Counter.tsx
@@ -0,0 +1,57 @@
import {
useReducer,
useContext,
createContext,
ReactNode,
Dispatch,
} from 'react'

type CounterState = number
type CounterAction =
| {
type: 'INCREASE' | 'DECREASE'
}
| {
type: 'INCREASE_BY'
payload: number
}

const CounterStateContext = createContext<CounterState>(0)
const CounterDispatchContext = createContext<Dispatch<CounterAction>>(
() => null
)

const reducer = (state: CounterState, action: CounterAction) => {
switch (action.type) {
case 'INCREASE':
return state + 1
case 'DECREASE':
return state - 1
case 'INCREASE_BY':
return state + action.payload
default:
throw new Error(`Unknown action: ${JSON.stringify(action)}`)
}
}

type CounterProviderProps = {
children: ReactNode
initialValue?: number
}

export const CounterProvider = ({
children,
initialValue = 0,
}: CounterProviderProps) => {
const [state, dispatch] = useReducer(reducer, initialValue)
return (
<CounterDispatchContext.Provider value={dispatch}>
<CounterStateContext.Provider value={state}>
{children}
</CounterStateContext.Provider>
</CounterDispatchContext.Provider>
)
}

export const useCount = () => useContext(CounterStateContext)
export const useDispatchCount = () => useContext(CounterDispatchContext)
9 changes: 7 additions & 2 deletions examples/with-context-api/package.json
Expand Up @@ -7,7 +7,12 @@
},
"dependencies": {
"next": "latest",
"react": "^17.0.2",
"react-dom": "^17.0.2"
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/node": "18.7.15",
"@types/react": "16.9.17",
"typescript": "4.8.2"
}
}
@@ -1,11 +1,10 @@
import type { AppProps } from 'next/app'
import { CounterProvider } from '../components/Counter'

function MyApp({ Component, pageProps }) {
export default function MyApp({ Component, pageProps }: AppProps) {
return (
<CounterProvider>
<Component {...pageProps} />
</CounterProvider>
)
}

export default MyApp
@@ -1,15 +1,16 @@
import type { MouseEvent } from 'react'
import Link from 'next/link'
import { useCount, useDispatchCount } from '../components/Counter'

const AboutPage = () => {
const count = useCount()
const dispatch = useDispatchCount()

const handleIncrease = (event) =>
const handleIncrease = (event: MouseEvent<HTMLButtonElement>) =>
dispatch({
type: 'INCREASE',
})
const handleIncrease15 = (event) =>
const handleIncrease15 = (event: MouseEvent<HTMLButtonElement>) =>
dispatch({
type: 'INCREASE_BY',
payload: 15,
Expand Down
@@ -1,15 +1,16 @@
import type { MouseEvent } from 'react'
import Link from 'next/link'
import { useCount, useDispatchCount } from '../components/Counter'

const IndexPage = () => {
const count = useCount()
const dispatch = useDispatchCount()

const handleIncrease = (event) =>
const handleIncrease = (event: MouseEvent<HTMLButtonElement>) =>
dispatch({
type: 'INCREASE',
})
const handleDecrease = (event) =>
const handleDecrease = (event: MouseEvent<HTMLButtonElement>) =>
dispatch({
type: 'DECREASE',
})
Expand Down
20 changes: 20 additions & 0 deletions examples/with-context-api/tsconfig.json
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"incremental": true,
"esModuleInterop": true,
"moduleResolution": "node",
"module": "esnext",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve"
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}