Function combineReducers

  • Given a dictionary of reducer functions creates a single reducer that once applied will call each reducer with its corresponding value of the state as its first argument and the event as its second.

    Returns

    single reducer function, this may be useful when we want to pass an event as argument to multiple reducers

    Example

    import { createEvent, createReducer } from 'reactive-actor';

    interface UsersState {
    auth: boolean;
    }

    const userInitialState: UsersState = {
    auth: false,
    };

    const authenticate = createEvent('[User] Authenticate');

    const userReducer = createReducer(userInitialState, (builder) =>
    builder.addCase(authenticate, (state) => ({ ...state, auth: true }))
    );

    interface RepositoriesState {
    show: boolean;
    }

    const repositoriesInitialState: RepositoriesState = {
    show: false,
    };

    const toggleRepositories = createEvent('[Repositories] Toggle Repositories');

    const repositoriesReducer = createReducer(repositoriesInitialState, (builder) =>
    builder.addCase(toggleRepositories, (state) => ({
    ...state,
    show: !state.show,
    }))
    );

    const reducer = combineReducers({
    users: userReducer,
    repositories: repositoriesReducer,
    });

    Type Parameters

    • S extends Record<K, S[K]>

    • T

    • K extends string | number | symbol

    Parameters

    • reducers: Record<K, Reducer<S[K], T>>

      a dictionary (object) of key (place to store result of the reduction), value (reducer function to apply against current state and event)

    Returns Reducer<S, T>

Generated using TypeDoc