liveBook · Manning

Redux

As explained in Essential TypeScript 5, Third Edition

Redux is a JavaScript library for managing application state in a centralized store through dispatched actions and reducer functions.

Redux centralizes application data in a store that React components read with selectors and change by dispatching actions handled by reducer functions. Redux Toolkit supplies the standard setup, while React-Redux connects the store to components. section 20.5 section 20.6

Build a typed Redux integration around these pieces:

  • Create a slice with a name, initial state, and reducer functions; export its generated reducer and actions. section 20.5
  • Configure the store with configureStore, then derive AppDispatch and RootState types for typed hooks. section 20.5
  • Wrap the application in React-Redux Provider, select state with useAppSelector, and dispatch actions with useAppDispatch. section 20.5
  • Use Redux Toolkit Query to define HTTP endpoints, add the API reducer and middleware, and expose generated query hooks. section 20.5.1

Product selections

In src/data/selectionSlice.ts, create a selections slice whose initial state is a ProductSelection array and whose addToOrder reducer accepts a product and quantity. In App.tsx, select state and dispatch reducers.addToOrder([product, quantity]). Register the reducer in src/data/dataStore.ts, then provide the store from src/index.tsx. section 20.5

Listing 20.22 The contents of the storeApis.ts file in the src/data folder
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
import { Product } from './entities';

const protocol = "http";
const hostname = "localhost";
const port = 4600;

const baseUrl = `${protocol}://${hostname}:${port}`;

export const productsApi = createApi({
   reducerPath: "products",
   baseQuery: fetchBaseQuery({baseUrl}),
   endpoints: (builder) => ({
       getProducts: builder.query<Product[], void>({
           query: () => "products"
       })
   })
})

export const { useGetProductsQuery } = productsApi;

Inspect a complete Redux Toolkit Query definition that creates a typed useGetProductsQuery hook for a products HTTP endpoint. section 20.5.1

How Essential TypeScript 5, Third Edition covers it

How deep it goes
Explains slices, store configuration, typed hooks, Provider wiring, and API middleware, while deferring broader store configurations.
Examples
Uses product selections, a ProductList component, generated products data, and a products HTTP endpoint.
What it assumes
Assumes a React project and TypeScript familiarity while demonstrating the Redux setup and annotations needed for the application.
What you can do after
Build a typed Redux store, connect it to React components, dispatch updates, and consume API data through generated query hooks.
How it connects
Connects Redux Toolkit with React, React-Redux, reducers, actions, hooks, and RESTful web services.

Also explained in

Compare how 2 books explain Redux →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Essential TypeScript 5, Third Edition. The section links point to the parts of the book it draws on.