SlideShare a Scribd company logo
1 of 78
Download to read offline
Intro to React & Redux
Boris Dinkevich
http://500Tech.com
boris@500tech.com
Boris Dinkevich
- React evangelist
- Past Angular hippie
- CoAuthor of 

The Complete Redux Book
CoFounder @ 500Tech
ES2015 (ES6)
Const & Let
Strings
Arrow functions
Destructuring
A WORD ON TOOLS
npm - package repository
babel - transpiler
webpack - build tool
BACK TO REACT
HISTORY
Component Driven Development
Thinking in components
Thinking in components
Lifecycle methods
Mount
componentWillMount → Angular PreLink
componentDidMount → Angular PostLink
Update
componentWillUpdate
componentDidUpdate
Unmount
componentWillUnmount → $scope.$on('destroy')
Virtual DOM
Recipe
Ingredients
Eggs
Virtual DOM
Recipe
Ingredients
Eggs
Real DOM
=
Recipe
Ingredients
Eggs
Recipe
Ingredients
MilkEggs
New Virtual DOM
Recipe
Ingredients
Eggs
Old Virtual DOM
Real DOM
!=
JSX
https://babeljs.io/repl/
Play with JSX online
=
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>

);

}
PROPS
Passing Props
const Add = (props) => (

<h2>Its: { props.a + props.b }</h2>

);



const App = () => (

<Add a={ 2 } b={ 3 } />

);
Repeating with JavaScript
(3/3)
const Recipes = ({ recipes }) => (

<div>

<h1>Recipes</h1>

<ul>

{

recipes.map((recipe) => <Recipe recipe={ recipe } />)

}

</ul>

</div>

);
CLASSES
Make <App /> into a class
class App extends React.Component {

render() {

return (

<div>

<Recipes recipes={ recipes }/>

</div>

);

}

}

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
CHANGE DETECTION
Change detection
Props change
State change - setState()
Forced - forceUpdate()
Add a recipe after 1sec
constructor() {

super();



setTimeout(() => {

console.log(`Adding new recipe`);

recipes.push('Shakshuka');

}, 1000);

}
PROP TYPES
Setting PropTypes
AddRecipe.propTypes = {

addRecipe: React.PropTypes.func.isRequired

};
add-recipe.js
https://facebook.github.io/react/docs/reusable-components.html
FLUX
MVC
FLUX
Flux
Components
Dispatcher
ActionsStores
Game engine
Store
Recipes
Waffles
App
Add RecipeRecipes
Recipe…Omelette Pancakes Recipe… Recipe…
REDUX
Click
Timeout
AJAX
Websocket
EVERYTHING IS AN ACTION
Add Recipe
Toggle Favorite
Fetch Recipes
Start Network
Current State
Next State
Reducers
(processors)
Action
Many reducers can be chained
Must return a new state — never modify previous one
Object.assign or Immutable
Only one store
REDUCERS
CONNECT
State to React
Store
Recipes
Recipe 1 Recipe 2
App
Recipes Add Recipe
Recipe 1 Recipe 2
State to React
Store
Recipes
Recipe 1 Recipe 2
App
Recipes Add Recipe
Recipe 1 Recipe 2
State to React
Store
Recipes
Recipe 1 Recipe 2
App
Recipes Add Recipe
Recipe 1 Recipe 2
Connect




connect()(App);

Connect
function mapStateToProps(state) { 

return {};

}



function mapDispatchToProps(dispath) {

return {};

}



connect(mapStateToProps, mapDispatchToProps)(App);

MUTATE STATE
OUR STATE
State
Recipes
Omelette Pancaek
User
Boris
T1
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;

};
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;

};
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;

};



Object.assign()
const original = {

name: 'Cat',

age: 3

};



const updated = Object.assign({}, original, {

name: 'Dog'

});
updated

> { name: 'Dog', age: 3 }



updated === original

> false
REFERENCE TREES
State
Recipes
Omelette Pancaek
User
Boris
T1
Pancake
T2
REFERENCE TREES
State
Recipes
Omelette Pancaek
User
Boris
T1
Recipes
Omelette Pancake
T2
REFERENCE TREES
State
Recipes
Omelette Pancaek
User
Boris
T1
State
Recipes
Omelette Pancake
User
Boris
T2
IN MEMORY
State
Recipes
Omelette Pancaek
User
Boris
State
Recipes
Pancake
T1 T2
ACTION #2
State
Recipes
Omelette Pancaek
User
Boris
State
Recipes
Omelett Pancake
User
Boris
T1 T2
State
Recipes
Bacon Pancake
User
Boris
T3
IN MEMORY
State
Recipes
Omelette Pancaek
User
Boris
State
Recipes
Pancake
State
Recipes
Bacon
T1 T2 T3
Why not direct?
state.recipes[recipeId].name = newName;
OH, NO!
State
Recipes
Omelette Pancaek
User
Boris
State
Recipes
Pancake
State
Recipes
Bacon
T1 T2 T3
Omelett
User
Boris
User
Cat Pancake
OUT SIDE CHANGE
State
Recipes
Omelette Pancaek
User
Cat
State
Recipes
Pancake
State
Recipes
Bacon
T1 T2 T3
OH, NO!
State
Recipes
Omelette Pancaek
User
Cat
State
Recipes
Pancake
State
Recipes
Bacon
T1 T2 T3
Omelett
User
Cat
User
Cat Pancake
Console work
s1 = store.getState()
- Do an action
s2 = store.getState()
- Do an action
s3 = store.getState()
store.dispatch({ type: ‘SET_STATE’, payload: s1 });
Console work
s1 = store.getState()
- Do an action
s2 = store.getState()
- Do an action
s3 = store.getState()
store.dispatch({ type: ‘SET_STATE’, payload: s1 });
Console work
s1 = store.getState()
- Do an action
s2 = store.getState()
- Do an action
s3 = store.getState()
store.dispatch({ type: ‘SET_STATE’, payload: s1 });
Console work
s1 = store.getState()
- Do an action
s2 = store.getState()
- Do an action
s3 = store.getState()
store.dispatch({ type: ‘SET_STATE’, payload: s1 });
UNDO / REDO
BUT…
1. Actions like LOGIN
2. Actions from Middleware / Redux-Thunk
3. Layout / UI
Directory structure
reducers
store
components
ACTION CREATORS
Actions
export const addRecipe = (name) => ({

type: 'ADD_RECIPE',

name: name

});
ROUTING
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')

);
TESTS
Testing
Karma - 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
SUMMARY
Useful stuff
Meetup: 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
Read our blog:
http://blog.500tech.com
React & Redux

More Related Content

What's hot

What's hot (20)

Introduction to Redux
Introduction to ReduxIntroduction to Redux
Introduction to Redux
 
Let's discover React and Redux with TypeScript
Let's discover React and Redux with TypeScriptLet's discover React and Redux with TypeScript
Let's discover React and Redux with TypeScript
 
React state managmenet with Redux
React state managmenet with ReduxReact state managmenet with Redux
React state managmenet with Redux
 
Designing applications with Redux
Designing applications with ReduxDesigning applications with Redux
Designing applications with Redux
 
Academy PRO: React JS. Redux & Tooling
Academy PRO: React JS. Redux & ToolingAcademy PRO: React JS. Redux & Tooling
Academy PRO: React JS. Redux & Tooling
 
Intro to ReactJS
Intro to ReactJSIntro to ReactJS
Intro to ReactJS
 
Redux training
Redux trainingRedux training
Redux training
 
Introduction to react and redux
Introduction to react and reduxIntroduction to react and redux
Introduction to react and redux
 
Redux js
Redux jsRedux js
Redux js
 
React with Redux
React with ReduxReact with Redux
React with Redux
 
React / Redux Architectures
React / Redux ArchitecturesReact / Redux Architectures
React / Redux Architectures
 
Intro to Redux | DreamLab Academy #3
Intro to Redux | DreamLab Academy #3 Intro to Redux | DreamLab Academy #3
Intro to Redux | DreamLab Academy #3
 
Quick start with React | DreamLab Academy #2
Quick start with React | DreamLab Academy #2Quick start with React | DreamLab Academy #2
Quick start with React | DreamLab Academy #2
 
Better web apps with React and Redux
Better web apps with React and ReduxBetter web apps with React and Redux
Better web apps with React and Redux
 
Building React Applications with Redux
Building React Applications with ReduxBuilding React Applications with Redux
Building React Applications with Redux
 
Redux vs Alt
Redux vs AltRedux vs Alt
Redux vs Alt
 
React & Redux
React & ReduxReact & Redux
React & Redux
 
Advanced redux
Advanced reduxAdvanced redux
Advanced redux
 
React & redux
React & reduxReact & redux
React & redux
 
React redux
React reduxReact redux
React redux
 

Viewers also liked

Viewers also liked (11)

Using ReactJS in AngularJS
Using ReactJS in AngularJSUsing ReactJS in AngularJS
Using ReactJS in AngularJS
 
React and redux
React and reduxReact and redux
React and redux
 
An Introduction to ReactJS
An Introduction to ReactJSAn Introduction to ReactJS
An Introduction to ReactJS
 
A Brief Introduction to React.js
A Brief Introduction to React.jsA Brief Introduction to React.js
A Brief Introduction to React.js
 
Reactjs
Reactjs Reactjs
Reactjs
 
ReactJS presentation
ReactJS presentationReactJS presentation
ReactJS presentation
 
Redux with angular 2 - workshop 2016
Redux with angular 2 - workshop 2016Redux with angular 2 - workshop 2016
Redux with angular 2 - workshop 2016
 
React + Redux Introduction
React + Redux IntroductionReact + Redux Introduction
React + Redux Introduction
 
Getting Started with React
Getting Started with ReactGetting Started with React
Getting Started with React
 
React JS and why it's awesome
React JS and why it's awesomeReact JS and why it's awesome
React JS and why it's awesome
 
React js
React jsReact js
React js
 

Similar to Introduction to React & Redux

[Final] ReactJS presentation
[Final] ReactJS presentation[Final] ReactJS presentation
[Final] ReactJS presentation
洪 鹏发
 

Similar to Introduction to React & Redux (20)

[Final] ReactJS presentation
[Final] ReactJS presentation[Final] ReactJS presentation
[Final] ReactJS presentation
 
React & Redux for noobs
React & Redux for noobsReact & Redux for noobs
React & Redux for noobs
 
react-hooks.pdf
react-hooks.pdfreact-hooks.pdf
react-hooks.pdf
 
Workshop React.js
Workshop React.jsWorkshop React.js
Workshop React.js
 
Integrating React.js with PHP projects
Integrating React.js with PHP projectsIntegrating React.js with PHP projects
Integrating React.js with PHP projects
 
The Functional Programming Toolkit (NDC Oslo 2019)
The Functional Programming Toolkit (NDC Oslo 2019)The Functional Programming Toolkit (NDC Oslo 2019)
The Functional Programming Toolkit (NDC Oslo 2019)
 
Combining Angular and React Together
Combining Angular and React TogetherCombining Angular and React Together
Combining Angular and React Together
 
Intro react js
Intro react jsIntro react js
Intro react js
 
React & The Art of Managing Complexity
React &  The Art of Managing ComplexityReact &  The Art of Managing Complexity
React & The Art of Managing Complexity
 
Build web apps with react js
Build web apps with react jsBuild web apps with react js
Build web apps with react js
 
Introduction to react
Introduction to reactIntroduction to react
Introduction to react
 
Lec9Handout.ppt
Lec9Handout.pptLec9Handout.ppt
Lec9Handout.ppt
 
Painless Migrations from Backbone to React/Redux
Painless Migrations from Backbone to React/ReduxPainless Migrations from Backbone to React/Redux
Painless Migrations from Backbone to React/Redux
 
React Native - Getting Started
React Native - Getting StartedReact Native - Getting Started
React Native - Getting Started
 
Server side rendering with React and Symfony
Server side rendering with React and SymfonyServer side rendering with React and Symfony
Server side rendering with React and Symfony
 
React outbox
React outboxReact outbox
React outbox
 
Getting Started with React v16
Getting Started with React v16Getting Started with React v16
Getting Started with React v16
 
React 16: new features and beyond
React 16: new features and beyondReact 16: new features and beyond
React 16: new features and beyond
 
Workshop 19: ReactJS Introduction
Workshop 19: ReactJS IntroductionWorkshop 19: ReactJS Introduction
Workshop 19: ReactJS Introduction
 
React on Rails - RailsConf 2017 (Phoenix)
 React on Rails - RailsConf 2017 (Phoenix) React on Rails - RailsConf 2017 (Phoenix)
React on Rails - RailsConf 2017 (Phoenix)
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by Anitaraj
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDM
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 

Introduction to React & Redux