mirror of
https://github.com/aspnet/JavaScriptServices.git
synced 2025-12-27 20:17:30 +00:00
Remove 'redux-typed' from ReactReduxSpa template, making it more standard as a Redux application
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import { typeName, isActionType, Action, Reducer } from 'redux-typed';
|
||||
import { ActionCreator } from './';
|
||||
import { Action, Reducer, ThunkAction } from 'redux';
|
||||
|
||||
// -----------------
|
||||
// STATE - This defines the type of data maintained in the Redux store.
|
||||
@@ -13,25 +12,34 @@ export interface CounterState {
|
||||
// They do not themselves have any side-effects; they just describe something that is going to happen.
|
||||
// Use @typeName and isActionType for type detection that works even after serialization/deserialization.
|
||||
|
||||
@typeName("INCREMENT_COUNT")
|
||||
class IncrementCount extends Action {
|
||||
}
|
||||
interface IncrementCountAction { type: 'INCREMENT_COUNT' }
|
||||
interface DecrementCountAction { type: 'DECREMENT_COUNT' }
|
||||
|
||||
// Declare a 'discriminated union' type. This guarantees that all references to 'type' properties contain one of the
|
||||
// declared type strings (and not any other arbitrary string).
|
||||
type KnownAction = IncrementCountAction | DecrementCountAction;
|
||||
|
||||
// ----------------
|
||||
// ACTION CREATORS - These are functions exposed to UI components that will trigger a state transition.
|
||||
// They don't directly mutate state, but they can have external side-effects (such as loading data).
|
||||
|
||||
export const actionCreators = {
|
||||
increment: (): ActionCreator => (dispatch, getState) => {
|
||||
dispatch(new IncrementCount());
|
||||
}
|
||||
increment: () => <IncrementCountAction>{ type: 'INCREMENT_COUNT' },
|
||||
decrement: () => <DecrementCountAction>{ type: 'DECREMENT_COUNT' }
|
||||
};
|
||||
|
||||
// ----------------
|
||||
// REDUCER - For a given state and action, returns the new state. To support time travel, this must not mutate the old state.
|
||||
export const reducer: Reducer<CounterState> = (state, action) => {
|
||||
if (isActionType(action, IncrementCount)) {
|
||||
return { count: state.count + 1 };
|
||||
|
||||
export const reducer: Reducer<CounterState> = (state: CounterState, action: KnownAction) => {
|
||||
switch (action.type) {
|
||||
case 'INCREMENT_COUNT':
|
||||
return { count: state.count + 1 };
|
||||
case 'DECREMENT_COUNT':
|
||||
return { count: state.count - 1 };
|
||||
default:
|
||||
// The following line guarantees that every action in the KnownAction union has been covered by a case above
|
||||
const exhaustiveCheck: never = action;
|
||||
}
|
||||
|
||||
// For unrecognized actions (or in cases where actions have no effect), must return the existing state
|
||||
|
||||
Reference in New Issue
Block a user