Last active
April 12, 2020 12:40
-
-
Save Kepro/70b510b20c193428b86632221c3bfcb2 to your computer and use it in GitHub Desktop.
Redux Store + thunk + logger + redux dev tools + TypeScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const REDUCER_ACTION = 'REDUCER_ACTION'; | |
export type ReducerState = { | |
action: boolean | |
} | |
const defaultReducerState = { | |
action: false | |
} | |
export function setAction(enabled: boolean) { | |
return { | |
type: REDUCER_ACTION, | |
enabled, | |
} | |
} | |
export default function reducer(state: ReducerState = defaultReducerState, action: any = {}) { | |
switch (action.type) { | |
case REDUCER_ACTION: | |
return { ...state, action: action.enabled }; | |
default: return state; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import thunk, { ThunkMiddleware } from 'redux-thunk'; | |
import { AnyAction, applyMiddleware, compose, createStore, combineReducers } from 'redux'; | |
import logger from 'redux-logger'; | |
import reducer, { ReducerState } from './reducer' | |
// TODO: create actions | |
export type Actions = AnyAction; | |
export type RootState = { | |
reducer: ReducerState | |
} | |
const rootReducer = combineReducers<RootState>({ | |
reducer, | |
}); | |
// eslint-disable-next-line no-underscore-dangle | |
const composeEnhancers: typeof compose = (window as any).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose; | |
const store = createStore( | |
rootReducer, | |
composeEnhancers(applyMiddleware(thunk as ThunkMiddleware<RootState, Actions>, logger)), | |
); | |
export default store; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment