動くコード図鑑技術記事現場の渡り方キャリア論すべての記事About
TypeScript

createContextでContextとProviderを作る

出典: React×TypescriptでuseContext使ってReduxライクなState管理をしてみるcreateContextでContextとProviderを作る

createContextでContextとProviderを作る (typescript)#c5010fb65476
export const CounterContextProvider : FC = ({children}) => {
    const [state,dispatch] = useReducer<typeof CounterReducer>(CounterReducer,CounterInitialState);
    return(
        <CounterContext.Provider value={{
            count : state.count,
            dispatch : dispatch
        }
        }>
            {children}
        </CounterContext.Provider>
    )
};
▸ この snippet は実行結果未収録
▸ 実行結果は未収録です
  • id: #c5010fb65476
  • lines: 12
  • extracted: 2026-06-10

Source収録記事

この snippet は記事の「createContextでContextとProviderを作る」セクションに登場する。コードの前後の文脈・ハマりどころの解説は記事本文で。

同じ記事から

4
図鑑トップ