reduxjs / redux-toolkit

The official, opinionated, batteries-included toolset for efficient Redux development
https://redux-toolkit.js.org
MIT License
10.74k stars 1.18k forks source link

Error `enhancers` when init store with configureStore. #299

Closed VuongVu closed 4 years ago

VuongVu commented 4 years ago

I have an error with enhancers, it says: Type 'StoreEnhancer<{}, {}>[]' is not assignable to type 'StoreEnhancer<{}, {}>[] | ConfigureEnhancersCallback | undefined'.

My code is written in Typescript:

My configureStore:

import { configureStore, getDefaultMiddleware } from '@reduxjs/toolkit';
import { routerMiddleware } from 'connected-react-router';
import { createInjectorsEnhancer, forceReducerReload } from 'redux-injectors';
import createSagaMiddleware from 'redux-saga';

import history from './routers/history';
import createReducer from './reducers';
import rootSaga from './sagas';

declare const module: any;

export default (initialState = {}) => {
    const reduxSagaMonitorOptions = {};
    const sagaMiddleware = createSagaMiddleware(reduxSagaMonitorOptions);
    const { run: runSaga } = sagaMiddleware;

    const middleware = [...getDefaultMiddleware(), sagaMiddleware, routerMiddleware(history)];

    const enhancers = [
        createInjectorsEnhancer({
            createReducer,
            runSaga,
        }),
    ];

    const store = configureStore({
        reducer: createReducer(),
        preloadedState: initialState,
        middleware,
        enhancers,
    });

    sagaMiddleware.run(rootSaga);

    if (module.hot && env !== 'production') {
        module.hot.accept('./reducers', () => {
            forceReducerReload(store);
        });
    }

    return store;
};

And reducers:

import { combineReducers } from '@reduxjs/toolkit';
import { connectRouter } from 'connected-react-router';

import reducerLoadingApp from './components/common/loading/loadingApp/reducer';

import history from './routers/history';

export default (injectedReducers = {}) =>
    combineReducers({
        router: connectRouter(history),
        loadingApp: reducerLoadingApp,
        ...injectedReducers,
    });

Plz tell me what am i doing wrong or give me a solution. Thank you.

phryneas commented 4 years ago

From that code example, I can only guess that you have two different versions of redux installed that might have conflicting definitions of the StoreEnhancer type. You can find that out by running yarn why redux.

I could only tell you more if you could provide me with a CodeSandbox or similar with a runnable reproduction.

As a quick fix, you could also replace

const store = configureStore({
        reducer: createReducer(),
        preloadedState: initialState,
        middleware,
-        enhancers,
+        enhancers: enhancers as any,
    });

but of course you'd lose type checks for that line.

VuongVu commented 4 years ago

yep, thank you for your support. I reinstalled redux with the same version with redux-toolkit but it not work. I will detect it later when I have more free time.