Slide 1

Slide 1 text

A BRIEF INTRODUCTION TO REDUX @knicklabs

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

IMPERATIVE PROGRAMMING ➤ A program is expressed as a series of statements. ➤ Control-flow structures may be used to conditionally execute statements or execute statements repeatedly. ➤ As statements are executed, the state of a program changes. ➤ Data may be mutable.

Slide 4

Slide 4 text

const a = [1, 2, 3]; const b = a.push(4);

Slide 5

Slide 5 text

a == [1, 2, 3, 4] b == 4

Slide 6

Slide 6 text

FUNCTIONAL PROGRAMMING ➤ FP is declarative. A program expresses what things are not what to do. ➤ If you say a =5 you cannot later say a = 6 because 5 != 6. ➤ You can also declare that the sum of a list of numbers is the first number (head) plus the sum of the remainder (tail) as a function (f(x)). ➤ Functions have no side effects and data is immutable.

Slide 7

Slide 7 text

REFERENTAIL TRANSPARENCY ➤ A function calculates something and returns a result. ➤ Given the same input, a function always produces the same output. ➤ You can prove that a function is correct. ➤ You can string together correct functions to compose solutions to more complex problems.

Slide 8

Slide 8 text

const a = Immutable.List.of(1, 2, 3); const b = a.push(4 );

Slide 9

Slide 9 text

a == [1, 2, 3] b == [1, 2, 3, 4]

Slide 10

Slide 10 text

What does any of this have to do with Redux?

Slide 11

Slide 11 text

“Redux is a predictable state container for JavaScript apps. It helps you write applications that behave consistently, run in different environments (client, server, and native), and are easy to test. On top of that, it provides a great developer experience, such as live code editing combined with a time traveling debugger. You can use Redux together with React, or with any other view library. It is tiny (2kB) and has no dependencies. Source: https://github.com/rackt/redux

Slide 12

Slide 12 text

THE FIRST RULE OF REDUX THERE IS ONE STATE

Slide 13

Slide 13 text

// This can be your state: 1 // This can be your state:
 [1, 2, 3] // Or this can be your state: {todos: [{ task: ‘Do something’, completed: false }]}

Slide 14

Slide 14 text

function todosReducer(state = {}, action) { return state; }; // The state is stored inside a store. let store = createStore(reducer);

Slide 15

Slide 15 text

THE SECOND RULE OF REDUX THE STATE IS IMMUTABLE

Slide 16

Slide 16 text

READ-ONLY STATE ➤ You can not write to or modify the state. ➤ The only way you can get data into or remove data from the application is by dispatching an action. ➤ An action is just a plain javascript object. ➤ An action is the minimal description of how the state should change.

Slide 17

Slide 17 text

// This is an action
 { type: ‘RESET_FILTERS’ } // And so is this
 {
 type: ‘CREATE_TODO’,
 task: ‘Do that thing’
 }

Slide 18

Slide 18 text

// Create a store
 let store = createStore(todosReducer); // Dispatch action to get data into the 
 // application.
 store.dispatchAction({
 type: ‘CREATE_TODO’,
 task: ‘Do that thing’
 });

Slide 19

Slide 19 text

THE THIRD RULE OF REDUX DESCRIBE STATE MUTATIONS
 WITH PURE FUNCTIONS

Slide 20

Slide 20 text

THE REDUCER ➤ A pure function is one where the return value is determined by the inputs - it has referential transparency. ➤ The reducer is a pure function. ➤ The reducer takes the previous application state and an action as parameters. ➤ The reducer returns a new application state. ➤ It does not modify the previous application state!

Slide 21

Slide 21 text

function todosReducer(state = {}, action) { switch (action.type) {
 case ‘CREATE_TODO’:
 return Object.assign({}, state, {
 todos: state.todos.concat([{task: action.task}]) 
 });
 default:
 return state;
 } }; 
 let store = createStore(reducer); // Subscribe to updates store.subscribe(() => console.log(store.getState()) );

Slide 22

Slide 22 text

PUTTING IT ALL TOGETHER Action Creators Reducers Store Dispatch action Previous State & Action New State User 
 Interface Subscribe to state change

Slide 23

Slide 23 text

LEARN MORE ➤ The README
 https://github.com/rackt/redux ➤ The official docs
 http://redux.js.org/ ➤ Getting Started with Redux with Dan Abramov 
 https://egghead.io/series/getting-started-with-redux ➤ Live editing time travelling goodness
 https://github.com/gaearon/redux-devtools ➤ ImmutableJS
 https://facebook.github.io/immutable-js/

Slide 24

Slide 24 text

YOU MAY ALSO LIKE ➤ The Elm Language
 http://elm-lang.org/