Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Typing Reducers with useReducer | Advanced React Patterns with TypeScript
TypeScript Essentials for React

bookTyping Reducers with useReducer

When you use the useReducer hook in React, TypeScript allows you to precisely type both your reducer's state and the actions it can handle. This ensures your reducer logic is robust and less prone to errors. Begin by defining a type for your state object, which describes the data structure your reducer manages. For example, if you are building a simple counter, you might define your state type as:

type CounterState = {
  count: number;
};

The next step is to define the possible actions your reducer can handle. A recommended pattern is to use a discriminated union for your action types. This approach lets TypeScript enforce that every action type is handled explicitly, making your reducer safer and easier to maintain.

A discriminated union for action typing uses a common type property to distinguish between different action shapes. Consider the following example for a counter reducer:

type CounterAction =
  | { type: "increment" }
  | { type: "decrement" }
  | { type: "reset"; payload: number };

Here, each action is an object with a type property, and some actions (like "reset") can include additional data via a payload property. This structure allows TypeScript to narrow the action type inside your reducer function, providing type safety for both the action type and any associated data.

1. What is a discriminated union useful for in reducer action typing?

2. Why should you type both state and actions in useReducer?

question mark

What is a discriminated union useful for in reducer action typing?

Select the correct answer

question mark

Why should you type both state and actions in useReducer?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 6. Capítulo 2

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

Awesome!

Completion rate improved to 4.17

bookTyping Reducers with useReducer

Desliza para mostrar el menú

When you use the useReducer hook in React, TypeScript allows you to precisely type both your reducer's state and the actions it can handle. This ensures your reducer logic is robust and less prone to errors. Begin by defining a type for your state object, which describes the data structure your reducer manages. For example, if you are building a simple counter, you might define your state type as:

type CounterState = {
  count: number;
};

The next step is to define the possible actions your reducer can handle. A recommended pattern is to use a discriminated union for your action types. This approach lets TypeScript enforce that every action type is handled explicitly, making your reducer safer and easier to maintain.

A discriminated union for action typing uses a common type property to distinguish between different action shapes. Consider the following example for a counter reducer:

type CounterAction =
  | { type: "increment" }
  | { type: "decrement" }
  | { type: "reset"; payload: number };

Here, each action is an object with a type property, and some actions (like "reset") can include additional data via a payload property. This structure allows TypeScript to narrow the action type inside your reducer function, providing type safety for both the action type and any associated data.

1. What is a discriminated union useful for in reducer action typing?

2. Why should you type both state and actions in useReducer?

question mark

What is a discriminated union useful for in reducer action typing?

Select the correct answer

question mark

Why should you type both state and actions in useReducer?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 6. Capítulo 2
some-alt