Skip to Content

Old Way - Using Asynchronous Thunk Functions

Raw Redux এ async operations করার জন্য manual thunk functions লিখতে হয় এবং loading, success, error states আলাদা আলাদা handle করতে হয়।


কোড
redux-setup.js
const fetch = require("node-fetch"); const { createStore, applyMiddleware } = require("redux"); const thunkMiddleware = require("redux-thunk"); // Initial State const initialState = { loading: false, posts: [], error: "", }; // Action Creators const fetchPostsRequested = () => { return { type: "posts/requested", }; }; const fetchPostsSucceeded = (posts) => { return { type: "posts/succeeded", payload: posts, }; }; const fetchPostsFailed = (error) => { return { type: "posts/failed", payload: error, }; }; // Reducer const RD_reducer = (state = initialState, action) => { switch (action.type) { case "posts/requested": return { ...state, loading: true, error: "" }; case "posts/succeeded": return { ...state, loading: false, error: "", posts: action.payload }; case "posts/failed": return { ...state, loading: false, error: action.payload.message, posts: [], }; default: return state; } };
তিনটি আলাদা action creator লিখতে হয় — requested, succeeded, failed।

Old Way এর সমস্যা
Raw Redux Async এর সমস্যা: - অনেক boilerplate code - Manual loading states - তিনটি আলাদা action creator - Error handling complexity - Switch statements

State Flow
পরবর্তী পেজে দেখবো কীভাবে RTK এর createAsyncThunk এই সব সমস্যার সমাধান করে।
Last updated on