counter-example

This commit is contained in:
Nathan Bierema 2024-08-05 22:13:52 -04:00
parent 6ba4082b23
commit ddd2aac69c
4 changed files with 26 additions and 17 deletions

View File

@ -1,9 +1,14 @@
import { combineReducers } from 'redux';
import { combineReducers, Reducer } from 'redux';
import counter from './counter';
import { CounterAction } from '../actions/CounterActions';
const rootReducer = combineReducers({
const rootReducer: Reducer<
CounterState,
CounterAction,
Partial<CounterState>
> = combineReducers({
counter,
});
}) as any;
export interface CounterState {
counter: number;

View File

@ -2,34 +2,40 @@ import {
createStore,
applyMiddleware,
compose,
PreloadedState,
Reducer,
StoreEnhancer,
Middleware,
} from 'redux';
import { persistState } from '@redux-devtools/core';
import thunk from 'redux-thunk';
import rootReducer, { CounterState } from '../reducers';
import DevTools from '../containers/DevTools';
import { CounterAction } from '../actions/CounterActions';
function getDebugSessionKey() {
const matches = /[?&]debug_session=([^&#]+)\b/.exec(window.location.href);
return matches && matches.length > 0 ? matches[1] : null;
}
const enhancer = compose(
applyMiddleware(thunk),
const enhancer: StoreEnhancer = compose(
applyMiddleware(thunk as unknown as Middleware),
DevTools.instrument(),
persistState(getDebugSessionKey()),
);
export default function configureStore(
initialState?: PreloadedState<CounterState>,
) {
export default function configureStore(initialState?: Partial<CounterState>) {
const store = createStore(rootReducer, initialState, enhancer);
if (module.hot) {
module.hot.accept('../reducers', () =>
// eslint-disable-next-line @typescript-eslint/no-require-imports
store.replaceReducer(require('../reducers').default as Reducer),
store.replaceReducer(
require('../reducers').default as Reducer<
CounterState,
CounterAction,
Partial<CounterState>
>,
),
);
}

View File

@ -1,11 +1,9 @@
import { createStore, applyMiddleware, PreloadedState } from 'redux';
import { createStore, applyMiddleware, Middleware } from 'redux';
import thunk from 'redux-thunk';
import rootReducer, { CounterState } from '../reducers';
const enhancer = applyMiddleware(thunk);
const enhancer = applyMiddleware(thunk as unknown as Middleware);
export default function configureStore(
initialState?: PreloadedState<CounterState>,
) {
export default function configureStore(initialState?: Partial<CounterState>) {
return createStore(rootReducer, initialState, enhancer);
}

View File

@ -1,9 +1,9 @@
import { PreloadedState, Store } from 'redux';
import { Store } from 'redux';
import { CounterState } from '../reducers';
import { CounterAction } from '../actions/CounterActions';
const configureStore: (
initialState?: PreloadedState<CounterState>,
initialState?: Partial<CounterState>,
) => Store<CounterState, CounterAction> =
process.env.NODE_ENV === 'production'
? // eslint-disable-next-line @typescript-eslint/no-require-imports