DEV Community

Muthu Pandian
Muthu Pandian

Posted on • Updated on

How to use useReducer and useContext hooks with Typescript in React


Is your components are complex with too many states and props?. its time to use useReducer and useContext hooks now to make them simple and clean.

In this article, we will see how to use useReducer and useContext hooks together with typescript in a step-by-step guide. If you are not familiar with useReducer and useContext, have read through in ReactJS site

What is useReducer?.

What is useContext?.

To showcase the use of useReducer and useContext hooks, we will create a simple poker game app in React and manage the game state using useReducer/useContext hooks.Let's get started.
Note: All the code sample mentioned below can be found in the github repo here


  1. Create React app with typescript
    Let's create a React app using create-react-app

    npx create-react-app react-context-app --template typescript
    # or
    yarn create react-app react-context-app --template typescript

    Navigate to react-context-app and run yarn start command to start the app. Access the app http://localhost:3000

While adding new files follow the below folder structure, and refer to the Github repo if you need any information on the imports, css files


  1. Add State: Let's create a GameState that will hold the game state, the state will have players, gameName, winner details, and game status.

    • Create state.ts with GameState and initial state
    export interface GameState {
        players: Player[];
        gameName: string;
        winner: Player | null;
        gameStatus: Status;
    export enum Status {
        NotStarted = 'Not Started',
        InProgress = 'In Progress',
        Finished = 'Finished',
    export interface Player {
        name: string;
        id: number;
        status: Status;
        value?: number;
    export const initialGameState: GameState = {
        players: [],
        gameName: 'Game1',
        winner: null,
        gameStatus: Status.NotStarted,
  2. Add Actions: Now let's add required Actions types for the poker games, actions like adding a player to the game, resetting the game, and playing the game.

    • Create actions.ts with the below actions
    export enum ActionType {
    export interface AddPlayer {
        type: ActionType.AddPlayer;
        payload: Player;
    export interface SetPlayerValue {
        type: ActionType.SetPlayerValue;
        payload: { id: number; value: number };
    export interface ResetGame {
        type: ActionType.ResetGame;
    export type GameActions = AddPlayer | SetPlayerValue | ResetGame;
  3. Add Reducer: Let's add a reducer file that will update the state for specific/required actions and side effects(calculate winner, game status etc).

    • Create reducer.ts with the below functions
    export function gameReducer(state: GameState, action: GameActions): GameState {
        switch (action.type) {
            case ActionType.AddPlayer:
            return { ...state, players: [action.payload, ...state.players] };
            case ActionType.ResetGame:
            return {
                players: => ({
                status: Status.NotStarted,
                value: 0,
            case ActionType.SetPlayerValue:
            let newState = {
                players: Player) =>
                    ? {
                        value: action.payload.value,
                        status: Status.Finished,
                    : player
            return {
                winner: getWinner(newState.players),
                gameStatus: getGameStatus(newState),
            return state;
    const getWinner = (players: Player[]): Player | null => {
        let winnerValue = 0;
        let winner = null;
        players.forEach((player) => {
            if (player.value && player.value > winnerValue) {
            winner = player;
            winnerValue = player.value || 0;
        return winner;
    const getGameStatus = (state: GameState): Status => {
        const totalPlayers = state.players.length;
        let numberOfPlayersPlayed = 0;
        state.players.forEach((player) => {
            if (player.status === Status.Finished) {
        if (numberOfPlayersPlayed === 0) {
            return Status.NotStarted;
        if (totalPlayers === numberOfPlayersPlayed) {
            return Status.Finished;
        return Status.InProgress;
    // helper functions to simplify the caller
    export const addPlayer = (player: Player): AddPlayer => ({
        type: ActionType.AddPlayer,
        payload: player,
    export const setPlayerValue = (id: number, value: number): SetPlayerValue => ({
        type: ActionType.SetPlayerValue,
        payload: { id, value },
    export const resetGame = (): ResetGame => ({
        type: ActionType.ResetGame,
  4. Add Context: Now let's add a context file

    • Create context.ts with below GameContext that uses the above-created State
        export const GameContext = React.createContext<{
        state: GameState;
        dispatch: React.Dispatch<GameActions>;
            state: initialGameState,
            dispatch: () => undefined,
  5. Add useContext and useReducer hook to the App: Now that we have created the necessary context, state etc, we can add them into the app.

    • Create a new component Poker.tsx for the poker game and add Context and useReducer hook as below. Ignore the errors for <PlayerList />, <Players /> , <GameStatus /> and <AddPlayer /> components for now, we will add these components in the coming steps. GameContext.Provider is the context provider here, any child component under this provider will have access to the context(i.e state and dispatch)
    export const Poker = () => {
        const [state, dispatch] = useReducer(gameReducer, initialGameState);
        return (
            <GameContext.Provider value={{ state, dispatch }}>
                <div className='Header'>
                        <p>React useReducer and useContext example Poker App</p>
                <div className='ContentArea'>
                    <div className='LeftPanel'>
                        <PlayersList />
                    <div className='MainContentArea'>
                        <Players />
                    <div className='RightPanel'>
                        <GameStatus />
                <div className='Footer'>
                    <AddPlayer />

    Add the <Poker/> component to App.tsx component file.

  6. Add Components: It's time to add the components and play the game.

  • Add AddPlayer.tsx component: This component will be responsible for adding new players to the game and updating the GameState using dispatch actions. We can access the GameState/Reducer using the useContext Hook here, useContext(GameContext)

    export const AddPlayer = () => {
        const { dispatch } = useContext(GameContext);
        const [playerName, setPlayerName] = useState('');
        const handlePlayerNameChange = (event: ChangeEvent<HTMLInputElement>) => {
        const handleSubmit = (event: FormEvent) => {
            const player: Player = {
            id: +new Date(),
            name: playerName,
            status: Status.NotStarted,
        return (
            <h4>Add New Player</h4>
            <form onSubmit={handleSubmit}>
                <button type='submit' value='Submit' disabled={playerName === ''}>
  • Add PlayersList.tsx component: This component will show a list of players in the game. Again we use the amazing useContext hook to get list players from GameState.

    export const PlayersList = () => {
        const { state } = useContext(GameContext);
        return (
            <div className='PlayersList'>
            { => (
  • Add Players.tsx component: This is the player's play area component. The component will show the player's status, card value, and a button to play the game. Again we use the amazing useContext hook to get players status from GameState and dispatch player action.

    export const Players = () => {
        const { state, dispatch } = useContext(GameContext);
        const playPlayer = (id: number) => {
            const randomValue = Math.floor(Math.random() * 100);
            dispatch(setPlayerValue(id, randomValue));
        return (
            <h4>Players Status</h4>
            <div className='PlayersContainer'>
                { Player) => (
                <div key={} className='Player'>
                    <label>Name: {}</label>
                    <label>Status : {player.status}</label>
                    <label>Card Value: {player.value}</label>
                    disabled={player.status !== Status.NotStarted}
                    onClick={() => playPlayer(}
                    Show Card
  • Add GameStatus.tsx component. Now finally we need to add a component to display the game status and winner information. The component also has a button to restart/reset the game, when the game is reset it clears all players card value and reset the game status(refer the reducer file on how this is done)

    export const GameStatus = () => {
        const { state, dispatch } = useContext(GameContext);
        return (
            <div className='GameStatus'>
            <div className='Status'>
                <h4>Game Status</h4>
                <button onClick={() => dispatch(resetGame())}>Start New Game</button>
            <div className='Winner'>
                {state.gameStatus === Status.InProgress && (
                    In Lead : {state.winner?.name} by {state.winner?.value}
                {state.gameStatus === Status.Finished && (
                    Winner: {state.winner?.name} by {state.winner?.value}
  1. Add CSS file: copy the required CSS files from the github repo here:

  2. Play the Game: Once you have added all necessary components, CSS and states, you should be ready to play the game and see the use of useContext and useReducer hooks in action.


Hope you had fun creating useContext and useReducer hooks and playing the game. As you have seen, the components look clean with fewer props/state and easy to manage the state/actions using useContext hook.

Full working demo:

Github repo:

Top comments (6)

dhatguy profile image
Joseph Odunsi

Nice one

daiky218 profile image
Kevin Dai

AWESOME example!

pau1tuck profile image


andemosa profile image
Anderson Osayerie

Great post

abbatyya profile image

Well done, good job, bravo, excellent.
Thank you

lpastine profile image
Luciano Pastine

Great example. Neat explanation! :)