4466ba436b
## Description This PR ensures that the default prettier config is used for examples and templates. This config is compatible with `prettier@3` as well (upgrading prettier is bigger change that can be a future PR). ## Changes - Updated `.prettierrc.json` in root with `"trailingComma": "es5"` (will be needed upgrading to prettier@3) - Added `examples/.prettierrc.json` with default config (this will change every example) - Added `packages/create-next-app/templates/.prettierrc.json` with default config (this will change every template) ## Related - Fixes #54402 - Closes #54409
57 lines
1.3 KiB
TypeScript
57 lines
1.3 KiB
TypeScript
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);
|