Defining actions

First, we need some actions. We'll want to increment and decrement the counter, and we'll also want to reset it to zero. The first two requirements can be achieved with a single action (decrementing is just incrementing by a negative amount), so we'll need two actions, each identified by a constant:

// Source file: src/counterApp/counter.actions.js

/* @flow */

export const COUNTER_INCREMENT = "counter:increment";
export const COUNTER_RESET = "counter:reset";

export type CounterAction = {
type: string,
value?: number
};

export const reset = () =>
({
type: COUNTER_RESET
}: CounterAction);

export const increment = (inc: number) =>
({
type: COUNTER_INCREMENT,
value: inc
}: CounterAction);

export const decrement = (dec: number) =>
({
type: COUNTER_INCREMENT,
value: -dec
}: CounterAction);

// returning increment(-dec) would have worked as well

In fact, we should say that increment(), decrement(), and reset() are action creators; the actual actions are the values returned by those functions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset