Creating a Reducer

Learn to create a reducer.

We'll cover the following...

Reducer

A reducer is a function that will make the necessary changes to the state. It takes in the current state and the action being processed as parameters and returns the new state. In this section, we are going to implement a reducer. Let’s perform the following steps:

  1. One of the parameters in the reducer is the action that invoked the state change. Let’s create a union type containing all the action types that will represent the reducer action parameter:

Press + to interact
type QuestionsActions =
| ReturnType<typeof gettingUnansweredQuestionsAction>
| ReturnType<typeof gotUnansweredQuestionsAction>
| ReturnType<typeof gettingQuestionAction>
| ReturnType<typeof gotQuestionAction>
| ReturnType<typeof searchingQuestionsAction>
| ReturnType<typeof searchedQuestionsAction>;

We have used the ReturnType utility type to get the return type of the action functions. ReturnType expects a function type to be passed into it, so we use the typeof keyword to get the type of each function.

Note: When typeof is used ...