I'm trying to setup a front-end environment with react + redux + typescript, but I am struggling to get it work with combineReducers. I get an error: Argument of type is not assignable to parameter of type 'ReducersMapObject'. See the full error message below the code.
STATE: (types/index.tsx)
export namespace StoreState {
export type Enthusiasm = {
languageName: string;
enthusiasmLevel: number;
}
export type All = {
enthusiasm: Enthusiasm
}
}
STORE: (store.tsx)
import { createStore } from 'redux';
import reducers from './reducers/index';
import { StoreState } from './types/index';
let devtools: any = window['devToolsExtension'] ? window['devToolsExtension']() : (f:any)=>f;
const Store = createStore<StoreState.All>(reducers, devtools);
export default Store;
REDUCER: (/reducers/HelloReducer.tsx)
import { EnthusiasmAction } from '../actions';
import { StoreState } from '../types/index';
import { INCREMENT_ENTHUSIASM, DECREMENT_ENTHUSIASM } from '../constants/index';
export const enthusiasm = (state: StoreState.Enthusiasm,
action: EnthusiasmAction): StoreState.Enthusiasm => {
switch (action.type) {
case INCREMENT_ENTHUSIASM:
return { ...state, enthusiasmLevel: state.enthusiasmLevel + 1 };
case DECREMENT_ENTHUSIASM:
return { ...state, enthusiasmLevel: Math.max(1, state.enthusiasmLevel - 1) };
default:
return state;
}
}
COMBINE REDUCERS (/reducers/index.tsx)
import { StoreState } from '../types/index';
import * as enthusiasmReducer from './HelloReducer';
import { combineReducers } from 'redux';
const reducer = combineReducers<StoreState.All>({
enthusiasm: enthusiasmReducer
});
export default reducer;
You're passing the object with all of HelloReducer
's exports instead of just the reducer. There's a couple of ways to fix it. You can select the reducer:
const reducer = combineReducers<StoreState.All>({
enthusiasm: enthusiasmReducer.enthusiasm
});
or import only the reducer:
import {enthusiasm} from './HelloReducer';
..
const reducer = combineReducers({enthusiasm});
or add export default enthusiasm;
to HelloReducer.tsx
and change the import to
import enthusiasmReducer from './HelloReducer';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With