introduction to react & redux

78
Intro to React & Redux Boris Dinkevich http://500Tech.com [email protected]

Upload: boris-dinkevich

Post on 09-Jan-2017

268 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Introduction to React & Redux

Intro to React & Redux

Boris Dinkevich

http://500Tech.com

[email protected]

Page 2: Introduction to React & Redux

Boris Dinkevich

- React evangelist - Past Angular hippie - CoAuthor of The Complete Redux Book

CoFounder @ 500Tech

Page 3: Introduction to React & Redux

ES2015 (ES6)

Page 4: Introduction to React & Redux

Const & Let

Strings

Arrow functions

Destructuring

Page 5: Introduction to React & Redux

A WORD ON TOOLS

Page 6: Introduction to React & Redux

npm - package repository

babel - transpiler

webpack - build tool

Page 7: Introduction to React & Redux

BACK TO REACT

Page 8: Introduction to React & Redux

HISTORY

Page 9: Introduction to React & Redux

Component Driven Development

Page 10: Introduction to React & Redux

Thinking in components

Page 11: Introduction to React & Redux

Thinking in components

Page 12: Introduction to React & Redux

Lifecycle methods

Mount componentWillMount → Angular PreLink

componentDidMount → Angular PostLink

Update componentWillUpdate

componentDidUpdate

Unmount componentWillUnmount → $scope.$on('destroy')

Page 13: Introduction to React & Redux

Virtual DOM

Page 14: Introduction to React & Redux

Recipe

Ingredients

Eggs

Virtual DOM

Recipe

Ingredients

Eggs

Real DOM

=

Page 15: Introduction to React & Redux

Recipe

Ingredients

Eggs

Recipe

Ingredients

MilkEggs

New Virtual DOM

Recipe

Ingredients

Eggs

Old Virtual DOM

Real DOM

!=

Page 16: Introduction to React & Redux

JSX

Page 17: Introduction to React & Redux

https://babeljs.io/repl/

Play with JSX online

Page 18: Introduction to React & Redux

=

function App() { return React.createElement('div', null, [ React.createElement('h1', null, 'I am a component!'), React.createElement('h2', null, 'I am a sub title') ]); }

function App() { return ( <div> <h1>I am a component!</h1> <h2>I am a sub title</h2> </div> ); }

Page 19: Introduction to React & Redux

PROPS

Page 20: Introduction to React & Redux

Passing Props

const Add = (props) => ( <h2>Its: { props.a + props.b }</h2>); const App = () => ( <Add a={ 2 } b={ 3 } /> );

Page 21: Introduction to React & Redux

Repeating with JavaScript (3/3)

const Recipes = ({ recipes }) => ( <div> <h1>Recipes</h1> <ul> { recipes.map((recipe) => <Recipe recipe={ recipe } />) } </ul> </div>);

Page 22: Introduction to React & Redux

CLASSES

Page 23: Introduction to React & Redux

Make <App /> into a class

class App extends React.Component { render() { return ( <div> <Recipes recipes={ recipes }/> </div> ); } }

Page 24: Introduction to React & Redux

Component Lifecycle

class MyComponent extends React.Component { constructor() { } render() { } getInitialState() { } getDefaultProps() { }

componentWillMount() { } componentDidMount() { }

componentWillReceiveProps() { } shouldComponentUpdate() { }

componentWillUpdate() { } componentDidUpdate() { } componentWillUnmount() { }}

https://facebook.github.io/react/docs/component-specs.html

Page 25: Introduction to React & Redux

CHANGE DETECTION

Page 26: Introduction to React & Redux

Change detectionProps change

State change - setState()

Forced - forceUpdate()

Page 27: Introduction to React & Redux

Add a recipe after 1sec

constructor() { super(); setTimeout(() => { console.log(`Adding new recipe`); recipes.push('Shakshuka'); }, 1000); }

Page 28: Introduction to React & Redux

PROP TYPES

Page 29: Introduction to React & Redux

Setting PropTypes

AddRecipe.propTypes = { addRecipe: React.PropTypes.func.isRequired};

add-recipe.js

https://facebook.github.io/react/docs/reusable-components.html

Page 30: Introduction to React & Redux

FLUX

Page 31: Introduction to React & Redux

MVCFLUX

Page 32: Introduction to React & Redux

Flux

Page 33: Introduction to React & Redux

Components

Dispatcher

ActionsStores

Page 34: Introduction to React & Redux
Page 35: Introduction to React & Redux

Game engine

Store

Recipes

Waffles

App

Add RecipeRecipes

Recipe…Omelette Pancakes Recipe… Recipe…

Page 36: Introduction to React & Redux

REDUX

Page 37: Introduction to React & Redux

Click

Timeout

AJAX

Websocket

EVERYTHING IS AN ACTION

Add Recipe

Toggle Favorite

Fetch Recipes

Start Network

Page 38: Introduction to React & Redux

Current State

Next StateReducers (processors)

Action

Page 39: Introduction to React & Redux

Many reducers can be chained

Must return a new state — never modify previous one

Object.assign or Immutable

Only one store

REDUCERS

Page 40: Introduction to React & Redux

CONNECT

Page 41: Introduction to React & Redux

State to React

Store

Recipes

Recipe 1 Recipe 2

App

Recipes Add Recipe

Recipe 1 Recipe 2

Page 42: Introduction to React & Redux

State to React

Store

Recipes

Recipe 1 Recipe 2

App

Recipes Add Recipe

Recipe 1 Recipe 2

Page 43: Introduction to React & Redux

State to React

Store

Recipes

Recipe 1 Recipe 2

App

Recipes Add Recipe

Recipe 1 Recipe 2

Page 44: Introduction to React & Redux

Connect

connect()(App);

Page 45: Introduction to React & Redux

Connect

function mapStateToProps(state) { return {}; } function mapDispatchToProps(dispath) { return {}; } connect(mapStateToProps, mapDispatchToProps)(App);

Page 46: Introduction to React & Redux

MUTATE STATE

Page 47: Introduction to React & Redux

OUR STATE

State

Recipes

Omelette Pancaek

User

Boris

T1

Page 48: Introduction to React & Redux

The reducer

action = { type: 'RENAME_RECIPE', recipeId: 871, name: 'Pancake'};

const reducer = (state, action) => { switch (action.type) { case ‘RENAME_RECIPE': const { recipeId, newName } = action; state.recipes[recipeId].name = newName; return state; } return state; };

Page 49: Introduction to React & Redux

The reducer

action = { type: 'RENAME_RECIPE', recipeId: 871, name: 'Pancake'};

const reducer = (state, action) => { switch (action.type) { case ‘RENAME_RECIPE': const { recipeId, newName } = action; state.recipes[recipeId].name = newName; return state; } return state; };

Page 50: Introduction to React & Redux

The reducer

const reducer = (state, action) => { switch (action.type) { case ‘RENAME_RECIPE': const recipe = state.recipes[action.recipeId]; const newRecipe = Object.assign({}, recipe, { name: action.newName }); const newRecipes = Object.assign({}, state.recipes, { [action.recipeId]: newRecipe }); return Object.assign({}, state, { recipes: newRecipes }); } return state; };

Page 51: Introduction to React & Redux

Object.assign()

const original = { name: 'Cat', age: 3 }; const updated = Object.assign({}, original, { name: 'Dog'});

updated> { name: 'Dog', age: 3 } updated === original> false

Page 52: Introduction to React & Redux

REFERENCE TREES

State

Recipes

Omelette Pancaek

User

Boris

T1

Pancake

T2

Page 53: Introduction to React & Redux

REFERENCE TREES

State

Recipes

Omelette Pancaek

User

Boris

T1

Recipes

Omelette Pancake

T2

Page 54: Introduction to React & Redux

REFERENCE TREES

State

Recipes

Omelette Pancaek

User

Boris

T1

State

Recipes

Omelette Pancake

User

Boris

T2

Page 55: Introduction to React & Redux

IN MEMORY

State

Recipes

Omelette Pancaek

User

Boris

State

Recipes

Pancake

T1 T2

Page 56: Introduction to React & Redux

ACTION #2

State

Recipes

Omelette Pancaek

User

Boris

State

Recipes

Omelett Pancake

User

Boris

T1 T2

State

Recipes

Bacon Pancake

User

Boris

T3

Page 57: Introduction to React & Redux

IN MEMORY

State

Recipes

Omelette Pancaek

User

Boris

State

Recipes

Pancake

State

Recipes

Bacon

T1 T2 T3

Page 58: Introduction to React & Redux

Why not direct?

state.recipes[recipeId].name = newName;

Page 59: Introduction to React & Redux

OH, NO!

State

Recipes

Omelette Pancaek

User

Boris

State

Recipes

Pancake

State

Recipes

Bacon

T1 T2 T3

Omelett

User

Boris

User

Cat Pancake

Page 60: Introduction to React & Redux

OUT SIDE CHANGE

State

Recipes

Omelette Pancaek

User

Cat

State

Recipes

Pancake

State

Recipes

Bacon

T1 T2 T3

Page 61: Introduction to React & Redux

OH, NO!

State

Recipes

Omelette Pancaek

User

Cat

State

Recipes

Pancake

State

Recipes

Bacon

T1 T2 T3

Omelett

User

Cat

User

Cat Pancake

Page 62: Introduction to React & Redux

Console work

s1 = store.getState()

- Do an action

s2 = store.getState()

- Do an action

s3 = store.getState()

store.dispatch({ type: ‘SET_STATE’, payload: s1 });

Page 63: Introduction to React & Redux

Console work

s1 = store.getState()

- Do an action

s2 = store.getState()

- Do an action

s3 = store.getState()

store.dispatch({ type: ‘SET_STATE’, payload: s1 });

Page 64: Introduction to React & Redux

Console work

s1 = store.getState()

- Do an action

s2 = store.getState()

- Do an action

s3 = store.getState()

store.dispatch({ type: ‘SET_STATE’, payload: s1 });

Page 65: Introduction to React & Redux

Console work

s1 = store.getState()

- Do an action

s2 = store.getState()

- Do an action

s3 = store.getState()

store.dispatch({ type: ‘SET_STATE’, payload: s1 });

Page 66: Introduction to React & Redux

UNDO / REDO

Page 67: Introduction to React & Redux

BUT…1. Actions like LOGIN 2. Actions from Middleware / Redux-Thunk 3. Layout / UI

Page 68: Introduction to React & Redux

Directory structurereducers

store

components

Page 69: Introduction to React & Redux

ACTION CREATORS

Page 70: Introduction to React & Redux

Actions

export const addRecipe = (name) => ({ type: 'ADD_RECIPE', name: name});

Page 71: Introduction to React & Redux

ROUTING

Page 72: Introduction to React & Redux

React Router

import { Router, Route, browserHistory } from 'react-router'render( <Provider store={ store }> <Router history={ browserHistory }> <Route path="" components={ App }> <Route path="/add" component={ AddRecipe } /> <Route path="/" component={ Recipes } /> </Route> <Route path="*" component={ NotFound } /> </Router> </Provider>, document.getElementById('app') );

Page 73: Introduction to React & Redux

TESTS

Page 74: Introduction to React & Redux

TestingKarma - Test running - https://karma-runner.github.io/0.13/index.html

Enzyme - Test utils for React - https://github.com/airbnb/enzyme

Jasmine - Matcher and mocks - http://ricostacruz.com/cheatsheets/jasmine.html

Redux tests - http://redux.js.org/docs/recipes/WritingTests.html

Page 75: Introduction to React & Redux

SUMMARY

Page 76: Introduction to React & Redux

Useful stuffMeetup: http://www.meetup.com/ReactJS-Israel/

Redux tutorial: https://egghead.io/series/getting-started-with-redux

The code we built: https://bitbucket.org/500tech/react-redux-course

Page 77: Introduction to React & Redux
Page 78: Introduction to React & Redux

Read our blog:http://blog.500tech.com

React & Redux