Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Typing Reducers with useReducer | Advanced React Patterns with TypeScript
Quizzes & Challenges
Quizzes
Challenges
/
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

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 6. Kapitel 2

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Awesome!

Completion rate improved to 4.17

bookTyping Reducers with useReducer

Svep för att visa menyn

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

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 6. Kapitel 2
some-alt