Skip to main content

Module: core/dispatch

Functions

dispatch

dispatch<T>(target, action): void

Description

It is used for compatibility with redux actions, when a class state with actions is being migrated from the Redux boilerplate code to the Reactant. And it's often used in conjunction with createState().

Example

const type = 'count_increase';

interface CountAction {
type: typeof type;
state: number;
}

@injectable()
class Counter {
@state
count = createState<CountAction['state'], CountAction>(
($state = 0, $action) => ($action.type === type ? $action.state : $state)
);

increase() {
dispatch<CountAction>(this, {
type,
state: this.count + 1,
});
}
}

const app = createApp({
modules: [],
main: Counter,
render: () => {},
});

app.instance.increase();
expect(app.instance.count).toBe(1);

Type parameters

NameType
Textends AnyAction<T> = AnyAction

Parameters

NameType
targetThisService
actionT

Returns

void

Defined in

packages/reactant-module/src/core/dispatch.ts:47