Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React and Redux: Managing Redux Custom Middleware List

For my react app, I have built many custom middlewares and passed them in the applyMiddleware(). Because I have so many, the redux store file is looking a little congested. Is it a good practice to pass them all in the applyMiddleware() or import them in a separate file within a function and then pass that function in the applyMiddleware() to keep it clean?

// Redux store
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;

export const store = createStore(
  reducers,
  composeEnhancers(
    applyMiddleware(...xMdl, ...yMdl, ...zMdl, ...nAmountsOfMdl),
  )
);
like image 569
tim_woods Avatar asked Nov 28 '20 22:11

tim_woods


People also ask

Which middleware is best in Redux?

Redux thunk is the most popular middleware that allows you to call action creators, which then returns a function instead of an action object.

How do I add middleware to react Redux?

To apply a middleware in redux, we would need to require the applyMiddleware function from the redux library. import {createStore, applyMiddleware} from "redux"; In order to check that our middleware is hooked up correctly, we can start by adding a log to display a message when an action is dispatched.

Can we use Redux without middleware?

The answer is Yes! This blog will try to explain on how to implement async action calls in redux without the use of any middlewares. We will just implement two API calls to not to over complicate things. Create a new file called api.

What is applyMiddleware in react Redux?

middleware) Middleware is the suggested way to extend Redux with custom functionality. Middleware lets you wrap the store's dispatch method for fun and profit. The key feature of middleware is that it is composable.


1 Answers

I prefer to make groups for reducers using combineReducers. ill share my middleware setup, hope it's helpful for u!

store.config.js :

import rootReducer from '../_reducers'

export const history = createBrowserHistory()

export const store = configureStore();

export default function configureStore(preloadingState) {
  const composeEnhancer = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
  const store = createStore(
    rootReducer(history),
    preloadingState,
    composeEnhancer(
      applyMiddleware(
        routerMiddleware(history),
        thunk,
      ),
    ),
  )

  return store
}

index.js (in reducers folder) :

const rootReducer = (history) => combineReducers({
    router: connectRouter(history),
    single: combineReducers({
        post: postReducer,
        guide: guideReducer,
        course: courseReducer,
        lesson: lessonReducer,
        exercise: exerciseReducer,
    }),
    bank: combineReducers({
        posts: postsReducer,
        guides: guidesReducer,
        courses: coursesReducer,
        lessons: lessonsReducer,
        exercises: exercisesReducer,
    }),
    melodition: playerReducer,
    user: combineReducers({
        profile: profileReducer,
        favorites: favoriteReducer,
    }),
    snackbar: snackbarReducer,
    auth: authReducer,
});

export default rootReducer;

Note: I did this on a large project with connected-react-router, maybe it's not good for every project.

like image 141
b3hr4d Avatar answered Oct 01 '22 18:10

b3hr4d