Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting warning message 'getDefaultMiddleware' is deprecated

I am getting a getDefaultMiddleware is deprecated warning after updating "@reduxjs/toolkit": "^1.6.1" So how should I remove this warning. Do we have any other way to inject default middleware in the configureStore function?

import { configureStore, getDefaultMiddleware } from "@reduxjs/toolkit";
import reducer from "./reducer";
import api from "./middleware/api";
export default function storeConfigure() {
   const store = configureStore({
   reducer,
   middleware: [
      ...getDefaultMiddleware(), 
      api
    ],
  });
  return store;
}

Any help is appreciated thanks!

like image 401
TheParam Avatar asked Jul 22 '21 05:07

TheParam


People also ask

What is getDefaultMiddleware?

As part of that, getDefaultMiddleware includes some middleware that are added in development builds of your app only to provide runtime checks for two common issues: Immutability check middleware: deeply compares state values for mutations.

What is redux toolkit?

​ Redux Toolkit is our official, opinionated, batteries-included toolset for efficient Redux development. It is intended to be the standard way to write Redux logic, and we strongly recommend that you use it.

Why use getdefaultmiddleware instead of the imported version?

getDefaultMiddleware was originally just a separate export - we added the callback form of the middleware option that receives getDefaultMiddleware as its argument later. That callback completely supersedes the reason to use the imported version

What is getdefaultmiddleware in Redux toolkit?

One of the goals of Redux Toolkit is to provide opinionated defaults and prevent common mistakes. As part of that, getDefaultMiddleware includes some middleware that are added in development builds of your app only to provide runtime checks for two common issues:

How do I get the default middleware of a reducer?

The middleware option in configureStore accepts a callback function, and that callback will be given getDefaultMiddleware as its argument: const store = configureStore ( { reducer: rootReducer, middleware: (getDefaultMiddleware) => getDefaultMiddleware ().concat (logger), })

How do I customize the list of middleware in configurestore?

By default, configureStore adds some middleware to the Redux store setup automatically. If you want to customize the list of middleware, you can supply an array of middleware functions to configureStore:


2 Answers

The middleware option in configureStore accepts a callback function, and that callback will be given getDefaultMiddleware as its argument:

const store = configureStore({
  reducer: rootReducer,
  middleware: (getDefaultMiddleware) => getDefaultMiddleware().concat(logger),
})

Use that instead of the separately imported version.

like image 190
markerikson Avatar answered Oct 22 '22 21:10

markerikson


Can use like that:

const store = configureStore({
  reducer,
  middleware: (getDefaultMiddleware) => [...getDefaultMiddleware(), api],
})

A callback return an array of middle ware, it's will be cleaner and easy to read.

like image 28
James Tran Avatar answered Oct 22 '22 21:10

James Tran