🔧 AI Nachrichten OpenAI pauses $200 Pro tier as Astra demand strains capacity(11.09.2026 um 11:23 Uhr)
🔧 AI Nachrichten OpenAI seeks tougher AI rules. CIOs may feel the ripple effects(10.09.2026 um 12:11 Uhr)
🔧 AI Nachrichten The quiet reason CIOs are slowing AI down(11.09.2026 um 11:00 Uhr)
🔧 AI Nachrichten OpenAI pauses $200 Pro tier as Astra demand strains capacity(11.09.2026 um 11:19 Uhr)
🪟 Windows TippsWindows XP's Cursor Indicator Is Getting a Windows 11 Refresh(25.08.2026 um 13:00 Uhr)
🔧 AI Nachrichten OpenAI pauses $200 Pro tier as Astra demand strains capacity(11.09.2026 um 11:23 Uhr)
🔧 AI Nachrichten OpenAI seeks tougher AI rules. CIOs may feel the ripple effects(10.09.2026 um 12:11 Uhr)
🔧 AI Nachrichten The quiet reason CIOs are slowing AI down(11.09.2026 um 11:00 Uhr)
🔧 AI Nachrichten OpenAI pauses $200 Pro tier as Astra demand strains capacity(11.09.2026 um 11:19 Uhr)
🪟 Windows TippsWindows XP's Cursor Indicator Is Getting a Windows 11 Refresh(25.08.2026 um 13:00 Uhr)

🔧 Programmierung 🕛 vor 1 Jahr 4 Min Lesezeit
0

🚀 Creating a Feature-Rich Redux Store with Redux Toolkit and TypeScript

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

Redux Toolkit simplifies state management in modern React applications by providing a standardized way to write Redux logic. In this article, we'll walk through the creation of a ProductSlice to manage product data, categories, and wishlist functionality using Redux Toolkit with TypeScript.









1. Setting Up the Redux Store



First, configure your Redux store using configureStore from Redux Toolkit. This provides an enhanced development experience with built-in middlewares and devtools.



Code: store.ts




CODE
import { configureStore } from "@reduxjs/toolkit";
import productReducer from "./features/ProductSlice";

export const store = configureStore({
reducer: {
product: productReducer, // Adding the product slice reducer
},
});

export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;






Here:




  • RootState infers the global state shape.


  • AppDispatch simplifies typing when dispatching actions in components.










2. Adding Typed Hooks for Redux



To avoid repetitive typing, we'll create custom hooks that enforce the types of our state and dispatch.



Code: hooks.ts




CODE
import { TypedUseSelectorHook, useDispatch, useSelector } from "react-redux";
import { RootState, AppDispatch } from "./store";

export const useAppDispatch = () => useDispatch<AppDispatch>();
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;






Use these hooks instead of the default useSelector and useDispatch to ensure proper type inference.









3. Defining the Product Slice



The ProductSlice manages all product-related data:




  • Products


  • Categories


  • Wishlist




We'll define the initial state, actions, and reducers using Redux Toolkit's createSlice.



Code: ProductSlice.ts




CODE
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { ProductSlice } from "../../models/ProductSlice";
import { Product } from "../../models/Product";
import { Category } from "../../models/Category";

// Initial state
const initialState: ProductSlice = {
allProducts: [],
categories: [],
newProducts: [],
featuredProducts: [],
wishlist: [], // Ensure consistent naming convention
};

export const productSlice = createSlice({
name: "productSlice",
initialState,
reducers: {
updateNewList: (state, action: PayloadAction<Product[]>) => {
state.newProducts = action.payload;
},
updateFeaturedList: (state, action: PayloadAction<Product[]>) => {
state.featuredProducts = action.payload;
},
addToWishlist: (state, action: PayloadAction<Product>) => {
if (!state.wishlist.some((item) => item.id === action.payload.id)) {
state.wishlist.push(action.payload);
}
},
addCategories: (state, action: PayloadAction<Category[]>) => {
state.categories = action.payload;
},
addProducts: (state, action: PayloadAction<Product[]>) => {
state.allProducts = action.payload;
},
},
});

// Exporting actions
export const {
updateNewList,
updateFeaturedList,
addToWishlist,
addCategories,
addProducts,
} = productSlice.actions;

export default productSlice.reducer;






Highlights:






1. State Management




  • Tracks multiple lists (allProducts, newProducts, wishlist).






2. Actions




  • Each action updates specific parts of the state, maintaining immutability.

  • Example: addToWishlist checks for duplicates before adding a new product.






3. Immer Integration




  • Redux Toolkit uses Immer under the hood, allowing mutable-style updates that are safe and performant.









4. Models for Type Safety



We use TypeScript interfaces to ensure the data structure is consistent.



Code: Models




CODE
// Product.ts
export interface Product {
id: number;
name: string;
price: number;
category: string;
// Add other fields as needed
}

// Category.ts
export interface Category {
id: number;
name: string;
}

// ProductSlice.ts
export interface ProductSlice {
allProducts: Product[];
categories: Category[];
newProducts: Product[];
featuredProducts: Product[];
wishlist: Product[];
}












 5. Using the Product Slice in React



To demonstrate, here's an example of a component that displays and manages the wishlist.



Code: WishlistComponent.tsx




CODE
import React from "react";
import { useAppSelector, useAppDispatch } from "../store/hooks";
import { addToWishlist } from "../features/ProductSlice";

const WishlistComponent: React.FC = () => {
const wishlist = useAppSelector((state) => state.product.wishlist);
const dispatch = useAppDispatch();

const addProductToWishlist = () => {
const product = { id: 1, name: "Sample Product", price: 100, category: "Sample" };
dispatch(addToWishlist(product));
};

return (
<div>
<h2>Wishlist</h2>
<button onClick={addProductToWishlist}>Add to Wishlist</button>
<ul>
{wishlist.map((product) => (
<li key={product.id}>{product.name}</li>
))}
</ul>
</div>
);
};

export default WishlistComponent;






Key Points:




  • The component uses useAppSelector to access the state and useAppDispatch to dispatch actions.


  • The addToWishlist reducer ensures no duplicate products are added.










Conclusion



Redux Toolkit makes managing complex states straightforward, even in large applications. Combined with TypeScript, you gain the benefits of a strongly-typed, developer-friendly ecosystem. By following the structure outlined in this article, you can build scalable state management solutions with ease.



What are your thoughts on using Redux Toolkit with TypeScript? Share your experiences in the comments! 🚀

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
2 Quellen
OpenAI pauses $200 Pro tier as Astra demand strains capacity
1 Quelle
Swiss government explores replacing Microsoft 365 with open-source software
1 Quelle
OpenAI seeks tougher AI rules. CIOs may feel the ripple effects
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten 🚀 Creating a Feature-Rich Redux Store with Redux Toolkit and TypeScript

Thematisch verwandte Begriffe: Creating, FeatureRich, Redux, Store · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...