Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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)
- Loading branch information
Showing
7 changed files
with
92 additions
and
40 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
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
5 changes: 2 additions & 3 deletions
5
examples/with-context-api/pages/_app.js → examples/with-context-api/pages/_app.tsx
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
5 changes: 3 additions & 2 deletions
5
examples/with-context-api/pages/about.js → examples/with-context-api/pages/about.tsx
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
5 changes: 3 additions & 2 deletions
5
examples/with-context-api/pages/index.js → examples/with-context-api/pages/index.tsx
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"] | ||
} |