February 2019
Intermediate to advanced
204 pages
4h 52m
English
Let's implement a simple example to turn on and turn off the light. We can build a simple robot that just listens to commands and, based on the commands, performs some actions. For simplicity, suppose that our robot can only understand two commands, as follows:
Now, let's build our robotic function:
const tubeLight = (state = "OFF", action) => { switch (action.type) { case "TURN_ON": return "ON"; case "TURN_OFF": return "OFF"; default: return state; }};
This is a simple JavaScript function that takes the initial state and action as parameters and returns a state. That sounds like something familiar, doesn’t it? Yup; you are right. This is a simple reducer function.
In the first section, you learned ...
Read now
Unlock full access