Skip to content

Instantly share code, notes, and snippets.

@Kepro
Last active April 12, 2020 12:40
Show Gist options
  • Save Kepro/70b510b20c193428b86632221c3bfcb2 to your computer and use it in GitHub Desktop.
Save Kepro/70b510b20c193428b86632221c3bfcb2 to your computer and use it in GitHub Desktop.
Redux Store + thunk + logger + redux dev tools + TypeScript
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;
}
}
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