Unverified Commit 16655c79 authored by Henrik Wenz's avatar Henrik Wenz Committed by GitHub

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



## Documentation / Examples

- [x] Make sure the linting passes by running `pnpm lint`
- [x] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)
parent fbf1c97e
import { useReducer, useContext, createContext } from 'react' import {
useReducer,
useContext,
createContext,
ReactNode,
Dispatch,
} from 'react'
const CounterStateContext = createContext() type CounterState = number
const CounterDispatchContext = createContext() type CounterAction =
| {
type: 'INCREASE' | 'DECREASE'
}
| {
type: 'INCREASE_BY'
payload: number
}
const reducer = (state, action) => { const CounterStateContext = createContext<CounterState>(0)
const CounterDispatchContext = createContext<Dispatch<CounterAction>>(
() => null
)
const reducer = (state: CounterState, action: CounterAction) => {
switch (action.type) { switch (action.type) {
case 'INCREASE': case 'INCREASE':
return state + 1 return state + 1
...@@ -12,12 +30,20 @@ const reducer = (state, action) => { ...@@ -12,12 +30,20 @@ const reducer = (state, action) => {
case 'INCREASE_BY': case 'INCREASE_BY':
return state + action.payload return state + action.payload
default: default:
throw new Error(`Unknown action: ${action.type}`) throw new Error(`Unknown action: ${JSON.stringify(action)}`)
} }
} }
export const CounterProvider = ({ children }) => { type CounterProviderProps = {
const [state, dispatch] = useReducer(reducer, 0) children: ReactNode
initialValue?: number
}
export const CounterProvider = ({
children,
initialValue = 0,
}: CounterProviderProps) => {
const [state, dispatch] = useReducer(reducer, initialValue)
return ( return (
<CounterDispatchContext.Provider value={dispatch}> <CounterDispatchContext.Provider value={dispatch}>
<CounterStateContext.Provider value={state}> <CounterStateContext.Provider value={state}>
......
...@@ -7,7 +7,12 @@ ...@@ -7,7 +7,12 @@
}, },
"dependencies": { "dependencies": {
"next": "latest", "next": "latest",
"react": "^17.0.2", "react": "^18.2.0",
"react-dom": "^17.0.2" "react-dom": "^18.2.0"
},
"devDependencies": {
"@types/node": "18.7.15",
"@types/react": "16.9.17",
"typescript": "4.8.2"
} }
} }
import type { AppProps } from 'next/app'
import { CounterProvider } from '../components/Counter' import { CounterProvider } from '../components/Counter'
function MyApp({ Component, pageProps }) { export default function MyApp({ Component, pageProps }: AppProps) {
return ( return (
<CounterProvider> <CounterProvider>
<Component {...pageProps} /> <Component {...pageProps} />
</CounterProvider> </CounterProvider>
) )
} }
export default MyApp
import type { MouseEvent } from 'react'
import Link from 'next/link' import Link from 'next/link'
import { useCount, useDispatchCount } from '../components/Counter' import { useCount, useDispatchCount } from '../components/Counter'
...@@ -5,11 +6,11 @@ const AboutPage = () => { ...@@ -5,11 +6,11 @@ const AboutPage = () => {
const count = useCount() const count = useCount()
const dispatch = useDispatchCount() const dispatch = useDispatchCount()
const handleIncrease = (event) => const handleIncrease = (event: MouseEvent<HTMLButtonElement>) =>
dispatch({ dispatch({
type: 'INCREASE', type: 'INCREASE',
}) })
const handleIncrease15 = (event) => const handleIncrease15 = (event: MouseEvent<HTMLButtonElement>) =>
dispatch({ dispatch({
type: 'INCREASE_BY', type: 'INCREASE_BY',
payload: 15, payload: 15,
......
import type { MouseEvent } from 'react'
import Link from 'next/link' import Link from 'next/link'
import { useCount, useDispatchCount } from '../components/Counter' import { useCount, useDispatchCount } from '../components/Counter'
...@@ -5,11 +6,11 @@ const IndexPage = () => { ...@@ -5,11 +6,11 @@ const IndexPage = () => {
const count = useCount() const count = useCount()
const dispatch = useDispatchCount() const dispatch = useDispatchCount()
const handleIncrease = (event) => const handleIncrease = (event: MouseEvent<HTMLButtonElement>) =>
dispatch({ dispatch({
type: 'INCREASE', type: 'INCREASE',
}) })
const handleDecrease = (event) => const handleDecrease = (event: MouseEvent<HTMLButtonElement>) =>
dispatch({ dispatch({
type: 'DECREASE', type: 'DECREASE',
}) })
......
{
"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"]
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment