SlideShare uma empresa Scribd logo
1 de 87
Baixar para ler offline
React & Redux
Christoffer Noring
Google Developer Expert
Contents
React basics
Flux
Redux
CSS
Mixins
Router
Tooling and Best Practices covered by
Fabrice and Mathieu
React Basics
React lib
React.createClass({})
class Component extends React.Component
React DOM
ReactDOM.render(
[Component]/ [JSX Expression],
[element]
)
Render our app
Minimum Setup ES5
<script src="node_modules/react/dist/react.js"></script>
<script src="node_modules/react-dom/dist/react-dom.js"></script>
<script src=“https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.34/browser.min.js">
script>
<body>
<div id="example"></div>
<script type="text/babel">
ReactDOM.render(
);
</script>
</body>
<h1>Hello, world!</h1>,
document.getElementById('example')
Where to render
the appContent
JSX
Syntax extension to javascript
const element = <h1>Hello, world!</h1>;
Allows us to do things like:
Needs to be transpiled :
const element = (
<h1 className="greeting">
Hello, world!
</h1>
);
JSX
const element = React.createElement(
'h1',
{className: 'greeting'},
'Hello, world!'
);
ES5
JSX is your friend, you don’t have to use it
HTML in my javascript!!
Components
ES5 : React.createClass({})
Your first component
app.js index.html
var App = React.createClass({
render: function() {
return (
<div className="app">
Hello, world! I am an app component
</div>
);
}
});
ReactDOM.render(
<App />,
document.getElementById('example')
);
render() is like
a paint method
<body>
<div id="example"></div>
<script type="text/babel">
</script>
</body>
<h1>Hello, world!</h1>,
document.getElementById('examp
ReactDOM.render(
);
<script type="text/babel"
src="app.js"></script>
replace with
createClass(),
creates the component
Your second component
var CV = React.createClass({
render: function() {
return (
<div className="comments">
Showing your cv
</div>
);
}
});
var App = React.createClass({
render: function() {
return (
<CV />
);
}
});
Component within a component = component centric
programming
Call createClass() again
return CV component
from App component
Component, input
<Component prop={ data }>
this.props.prop
Component with data
let data = {
name : 'chris',
profession : 'software developer',
skills : ['.net', 'javascript', 'angular', 'react']
}
This is what we want to render
looks like a property
looks like a list
ReactDOM.render(
<App data={ data } />,
document.getElementById('example')
);
property = { variable }
Access data inside component like this:
var App = React.createClass({
render: function() {
return (
<div> { this.props.data.name } </div>
);
}
});
this.props.data.name
“chris”
Rendering a list
var App = React.createClass({
render: function() {
return (
<div>
<div> { this.props.data.name } </div>
<div>{ skills }</div>
</div>
);
}
});
let skills = this.props.data.skills.map(skill => {
return (
<div className="skill">{skill}</div>
);
});
Projection
Interpolation
Everything is in the “App” component = BAD
Refactoring
Putting everything in the App component isn’t very “react like”
of us
var App = React.createClass({
render: function() {
return (
<CV data={ this.props.data } />
);
}
});
var CV = React.createClass({
render: function() {
return (
<div className="cv">
{ this.data.props.name }
<Skills data={ this.data.props.skills } >
</div>
);
}
});
var Skills = React.createClass({
render : function(){
var skills = this.props.data.map(function(skill) {
return (
<div className="skill">{skill}</div>
);
});
return(
<div className="skills">
<h2>Skills</h2>
{ skills }
</div>
);
}
})
We can do this even better
var Skills = React.createClass({
render : function(){
var skills = this.props.data.map(function(skill) {
return (
<Skill data={ skill } />
);
});
return(
<div className="skills">
<h2>Skills</h2>
{ skills }
</div>
);
}
})
var Skill = React.createClass({
render : function(){
return(
<div className="skills">
<h3>{ this.props.data }</h3>
</div>
);
}
})
A component should do one thing well
One Skill comp
per skill item
Component tree so far
App
CV
Skills
Skill
Skill
Skill
etc…
What about methods?
<button type="submit"
onClick={this.onSubmit} >
Save new skills
</button>
var CV = React.createClass({
onSubmit : function(e){
e.preventDefault();
// do stuff
},
render : function(){
return (
)
}
Add method to
our object literal
Refer to method in
markup
State
Component State
var App = React.createClass({
getInitialState : function() {
return {
a : ‘some state’
};
},
render : function() {
return <div>{ this.state.a }</div>
}
});
Reading state
getInitialState is read
once per bootstrap,
define your state here
this.state.<prop>
Changing state
var App = React.createClass({
getInitialState : function() {
return {
newSkill : ‘some state’,
b : ‘some other state’
};
},
render : function() {
return <div>
<input value={this.state.newSkill} >
{ this.state.a }
</div>
}
For every change
of input field
update state
onChange={this.onSkillChange}
bind to onchange
onSkillChange : function(e){
this.setState({newSkill : e.target.value});
}
State summary
getInitialState(){ return { prop : ‘’ } }
{ this.state.prop }
this.setState({ prop : ‘newValue’ })
Lifecycle events
Initial State Changes Props changes
Unmounting
Initial
getDefaultProps
getInitialState
componentWillMount
render
componentDidMount
Set initial values
access DOM
fetch data
State changes
shouldComponentUpdate
componentWillUpdate
render
componentDidUpdate
boolean : is rerendering needed
prepare for update
perform DOM operations
Props change
componentWillReceiveProps
shouldComponentUpdate
componentWillUpdate
render
componentDidUpdate
called only when props
have changed
perform DOM operations
Unmounting
componentWillUnmount
is called before component is removed from DOM
do cleanup, think of it as a destructor
ES6
We want to write in ES6 cause it has nice features
Object.assign() Let Const
Spread Operator Lambdas
ES6 modules
import {} from ‘’
export default
export { a,b,c }
ES6 component
class Todo extends React.Component {
constructor(){
super();
this.action = this.action.bind(this);
this.state.prop = value
}
render() {
return (
<div onClick={this.action}>{this.props.title}</div>
)
}
action(e){
this.props.click( this.props.id );
}
}
GOTCHA, we need to call the following,
for our methods to be picked up
We inherit from React.Component
instead of
calling React.createClass({})
Otherwise its business as usual
we DON’t use getInitialState(){}
we just set this.state in constructor
We create a proper class
rather than an object literal
PropTypes
Catch bugs with type checking
Will give an error in a tool,
wrong type
Component.propTypes ={
title : React.PropTypes.string
}
<Component title=1 >
Example 1 - wrong type
Component.propTypes ={
title : React.PropTypes.string.isRequired
}
Example 2 - required
<Component > Will giver error, prop missing
Many validation types
oneOfType oneOf
arrayOf
objectOf
element instanceOf
symbol
custom
Further reading, https://facebook.github.io/react/docs/
typechecking-with-proptypes.html
Flux
Architecture pattern by Facebook
Action describes what
should happen with what data
Action
Dispatcher
Store
React e.g. Add todo
Store notifies listener that data
has changed and needs to be reread
Dispatch action to store, tell the store
Unidirectional flow, everything flows in one direction
Action
Dispatcher
Store
React
{ type : ADD_TODO, todo : { title : ‘dfdfd’ } }
What Payload
Dispatcher
Action
Dispatcher
Store
React
Dispatches actions dispatch(action)
Handles a dispatched action through
register(function(action){})
Store
Action
Dispatcher
Store
React
Contains your state, usually
same file have ability to perform
ajax and also notify listeners when
state has been updated
var Todos = React.createClass({
getInitialState : function(){
return { todos : Store.getTodos() }
}
render : function(){
return ([ render todos ])
},
})
//todos-component.js
componentDidMount : function(){
Store.addChangeListener( this._onChange )
},
componentWillUnmount : function(){
Store.removeChangeListener( this._onChange )
},
onChange() {
this.setState({ todos : Store.getTodos() })
}
Get data
1
Update state
so render() is called
2
store.js
var todos = [];
function loadTodos(){ return todos; }
var Store = merge(EventEmitter.prototype, {
getTodos : function(){ return todos; }
emitChange : function(){ emit(‘change’) },
addChangeListener : function(callback){ this.on(‘change’, callback) },
removeChangeListener : function(callback) { this.removeListener(‘change’, callback) }
})
Dispatcher.register(function(payload){
var action = payload.action;
switch(action) {
case ADD_TODO: todos.push( payload.data )
case LOAD_TODOS: loadTodos();
}
Store.emitChange();
})
Calls _onChange() on the component
Called when
Dispatcher.dispatch()
getData
addListener
removeListener
notifyChange
var AddTodo = React.createClass({
render : function(){
return ([ todo input ])
},
createTodo : function(todo){
Actions.addTodo( todo )
}
})
//add-todo-component.js
Calls the ActionCreator with a type and a payload
ActionCreator
Actions = {
addTodo : function(todo) {
Dispatcher.dispatch(
{ actionType : ADD_TODO, data : todo } )
}
…
}
Dispatch the action
Redux
Why Redux or
Flux growing pains
Scales well on larger complex apps
Boiler plate is way too big on smaller apps
Data flow
Action
Store
React
Reducers
No dispatcher!!
Action
Action
Store
React
Reducers
function addTodo( todo ) {
return { type : ‘ADD_TODO’, todo: todo }
}
Action Creator
Represent user intent
Must have a type
Redux Store
store.dispatch( action );
store.subscribe( listener );
store.getState()
replaceReducer( nextReducer )
Action
Store
React
Reducers
Reducers
Action
Store
React
Reducers
function reducer(state, action) {
return newState;
}
(state, action) => state
Must be pure
Multiple reducers per app
We want to change the state, without mutating so how?
Change object
Change Array
Change Object
What about a large object hierarchy?
Object.assign( target, oldObject, newState )
Object.assign(
{},
{ update : false, name : ‘’ },
{ update : true }
)
{ update : true, name : ‘’ }
Merged
Changing array
//mutable
state = [ { id : 1, name : 'tomato' } ]
state.push( { id: 2, name : 'cucumber' } );
return state;
Mutating
Point of mutation
//immutable
state = [
{ id : 1, name : 'tomato' }
]
return [
...state,
Object.assign({}, state, course)
]
Immutable
old array
+
new item
Spread operator …
Reducer no-no list
Mutate arguments
Perform side effects
Call non-pure functions
Benefits to immutable state
Clarity answers - who changed that state?
Mutable, anyone could have changed it
Immutable, only a reducer could have changed it
Performance
No need to check every single property on an object
if(oldState != newState), reference check
Time travel debugging
State Summary
ES5
lodash merge
lodash extend
Object-assign ( NPM )
Reference checking
is super fast
Enables time-travel
debugging
through browser plugin
Object.assign()
… spread operator for arrays
ES6
Change state by
All Reducers are called on each dispatch
addItemReducer
removeItemReducer
listItemsReducer
Dispatching addItem
{ type: ADD_ITEM, : item : item }
returns new state
return state
return state
Connect Redux to our App
react-redux
Action
Store
React
Reducers
Provider, attaches app to store
Connect, creates container items
Connect store data to our
app
ComponentApp
Provider
Store Data
Setup our store
Wrap App component in a Provider
import { combineReducers } from 'redux';
import todos from './todo-reducer';
const rootReducer = combineReducers({
todos : todos
}) // OR todos only, LHS is implied
export default rootReducer;
reducers/index.js
Root reducer
Combines all reducers in one,
this is one we feed to the store
Initialise store
import { createStore, applyMiddleware } from 'redux';
import rootReducer from '../reducers' // index.js
import reduxImmutableStateInvariant from 'redux-immutable-state-invariant';
export default function configureStore(initialState) {
return createStore(
rootReducer,
initialState,
applyMiddleware( reduxImmutableStateInvariant() )
)
}
configure-store.js
Create store
give it a
root reducer
initial state
and add middleware
Provider
<Provider store={store} >
<App />
</Provider>
Uses Reacts context - don’t touch
import configureStore from './store/configureStore';
import { Provider } from 'ReactRedux';
Make your store available to all your components
Component
Wrap our component in a container
component using
Connect
Presentational
Component
Container component
export default connect(
mapStateToProps,
mapDispatchToProps
)( PresentationalComponent )
what state
what functions
what state should I expose as props
mapStateToProps
mapStateToProps = () => {
return {
appState : ‘’,
otherAppState : ‘’
}
}
// Component
this.props.appState
this.props.otherAppState
Every time a change happens this function is rerun, so don’t
do anything expensive in there
mapDispatchToProps cont..
mapDispatchToProps = (dispatch) {
return {
loadTodos : () => {
dispatch( loadTodos() );
},
addTodo : (todo) => {
dispatch( addTodo(todo) );
}
}
}
Manual approach, but clear whats happening, recommended
when starting out
e.g. this.props.loadTodos();
mapDispatchToProps - nicer
what actions do we want to expose to the component
mapDispatchToProps = () => {
return {
actions : bindActionCreators(actions, dispatch)
}
}
This is a shorthand using Redux
this.actions.methodName()
Container component
Smart component
Focus on how things work
“Aware” of Redux
Subscribe to Redux state
Dispatch redux actions
react-redux lib
Presentational component
Normal component that show data through props,
knows nothing about Redux
Presentation component gets decorated
class TodoComponent extends React.Component{
onSave(){
this.props.dispatch(todoActions.addTodo(this.state.todo));
//console.log(this.state.todo.title);
}
}
function mapStateToProps(state, ownProps) {
return {
todos : state.todos
}
}
export default connect(
mapStateToProps)
(TodoComponent); 1
2
3
Reduxify your component in 3 steps
Decorate our Component
Expose state
Calling and dispatching
an action
Redux flow so far
Actions Dispatches an action
Reducers
current state + action =
new state
Store let connected components
know of state change
ReactRedux determine wether
to tell React to update UI
React new data passed
through props
YES
Something happenedReact
Redux concepts so far
Container and Presentation components
Has methods,
Has state,
Knows Redux
Just props
No Redux
ReactRedux
Provider Pass store data to Components
Connect
mapStateToProps Pass state and
methods to Components
mapDispatchToProps
Code Demo
Improving our Redux
Change our call to .connect()
export default connect(
mapStateToProps)
(TodoComponent);
export default connect(
mapStateToProps
mapDispatchToProps)
(TodoComponent);
function mapDispatchToProps(dispatch) {
return addTodo : todo => dispatch(todoActions.addTodo(todo))
}
Wrap the action in a dispatch()
onSave(){
this.props.dispatch(todoActions.addTodo(this.state.todo));
}
Change to
onSave(){
this.props.addTodo(this.state.todo));
}
All knowledge of dispatch is removed
Much cleaner
Dispatch() is no longer injected into the component
Fixing the component
And a little more…
import { bindActionCreators } from ‘redux’
This
function mapDispatchToProps(dispatch) {
return addTodo : todo => dispatch(todoActions.addTodo(todo))
}
Becomes this
function mapDispatchToProps(dispatch) {
return actions : bindActionCreators( todoActions, dispatch )
}
MAGIC !!
onSave(){
this.props.actions.addTodo(this.state.todo));
}
Clean up magic strings
export default {
ADD_TODO : 'ADD_TODO'
}
actionTypes.js
todo-actions.js
import actionTypes from './action-types';
function addTodo(todo) {
return { type : actionTypes.ADD_TODO, todo : todo };
}
todo-reducer.js
export default function todoReducer(state = [], action) {
switch(action.type) {
case actionTypes.ADD_TODO:
return [
...state,
Object.assign({}, action.todo)
]
And so on…
Replace string with constant
Async load
export function loadTodos() {
return function(dispatch) {
return service.getTodos().then( todos => {
dispatch( loadTodosSuccess(todos) )
}).catch(error => { console.error(error) })
}
}
export function loadTodosSuccess(todos) {
return { type : types.LOAD_TODOS, todos : todos }
}
todo-actions.js
index.js
const store = configureStore();
store.dispatch( loadTodos() )
set initial data
Call dispatch
when Ajax is done
export default function todoReducer(
state = [], action) {
switch(action.type) {
…
case actionTypes.LOAD_TODOS :
return action.todos
default :
return state;
}
}
todo-reducer.js
Component
class TodoComponent extends React.Component{
render(){
let todos = this.state.todos.map( todo => {
return <div>{ todo }</div>
})
return ( <div>{ todos }</div> )
}
}
function mapStateToProps(state, ownProps) {
return {
todos : state.todos
}
}
export default connect(
mapStateToProps)
(TodoComponent);
Async load summary
Create async action that calls
dispatch when done
Add reducer for loading data
Add said reducer to rootReducer
store.dispatch( loadAction() ) in index.js
ensure its exposed in mapStateToProps
use in presentation component
Thank you

Mais conteúdo relacionado

Mais procurados

Introduction to React JS for beginners
Introduction to React JS for beginners Introduction to React JS for beginners
Introduction to React JS for beginners Varun Raj
 
React Js Simplified
React Js SimplifiedReact Js Simplified
React Js SimplifiedSunil Yadav
 
React js programming concept
React js programming conceptReact js programming concept
React js programming conceptTariqul islam
 
ReactJS presentation
ReactJS presentationReactJS presentation
ReactJS presentationThanh Tuong
 
20180518 QNAP Seminar - Introduction to React Native
20180518 QNAP Seminar - Introduction to React Native20180518 QNAP Seminar - Introduction to React Native
20180518 QNAP Seminar - Introduction to React NativeEric Deng
 
React workshop presentation
React workshop presentationReact workshop presentation
React workshop presentationBojan Golubović
 
learn what React JS is & why we should use React JS .
learn what React JS is & why we should use React JS .learn what React JS is & why we should use React JS .
learn what React JS is & why we should use React JS .paradisetechsoftsolutions
 
Introduction to react and redux
Introduction to react and reduxIntroduction to react and redux
Introduction to react and reduxCuong Ho
 
Reactjs workshop
Reactjs workshop Reactjs workshop
Reactjs workshop Ahmed rebai
 
[Final] ReactJS presentation
[Final] ReactJS presentation[Final] ReactJS presentation
[Final] ReactJS presentation洪 鹏发
 
Introduction to React JS for beginners | Namespace IT
Introduction to React JS for beginners | Namespace ITIntroduction to React JS for beginners | Namespace IT
Introduction to React JS for beginners | Namespace ITnamespaceit
 

Mais procurados (20)

Introduction to React JS for beginners
Introduction to React JS for beginners Introduction to React JS for beginners
Introduction to React JS for beginners
 
React Js Simplified
React Js SimplifiedReact Js Simplified
React Js Simplified
 
React JS - Introduction
React JS - IntroductionReact JS - Introduction
React JS - Introduction
 
React/Redux
React/ReduxReact/Redux
React/Redux
 
React js programming concept
React js programming conceptReact js programming concept
React js programming concept
 
React js
React jsReact js
React js
 
ReactJS presentation
ReactJS presentationReactJS presentation
ReactJS presentation
 
20180518 QNAP Seminar - Introduction to React Native
20180518 QNAP Seminar - Introduction to React Native20180518 QNAP Seminar - Introduction to React Native
20180518 QNAP Seminar - Introduction to React Native
 
Its time to React.js
Its time to React.jsIts time to React.js
Its time to React.js
 
Intro to React
Intro to ReactIntro to React
Intro to React
 
React workshop presentation
React workshop presentationReact workshop presentation
React workshop presentation
 
learn what React JS is & why we should use React JS .
learn what React JS is & why we should use React JS .learn what React JS is & why we should use React JS .
learn what React JS is & why we should use React JS .
 
Introduction to react and redux
Introduction to react and reduxIntroduction to react and redux
Introduction to react and redux
 
Intro to React
Intro to ReactIntro to React
Intro to React
 
Reactjs workshop
Reactjs workshop Reactjs workshop
Reactjs workshop
 
NEXT.JS
NEXT.JSNEXT.JS
NEXT.JS
 
[Final] ReactJS presentation
[Final] ReactJS presentation[Final] ReactJS presentation
[Final] ReactJS presentation
 
React js
React jsReact js
React js
 
Workshop React.js
Workshop React.jsWorkshop React.js
Workshop React.js
 
Introduction to React JS for beginners | Namespace IT
Introduction to React JS for beginners | Namespace ITIntroduction to React JS for beginners | Namespace IT
Introduction to React JS for beginners | Namespace IT
 

Destaque

React + Redux Introduction
React + Redux IntroductionReact + Redux Introduction
React + Redux IntroductionNikolaus Graf
 
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 awesomeAndrew Hull
 
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 #2DreamLab
 
Finjs - Angular 2 better faster stronger
Finjs - Angular 2 better faster strongerFinjs - Angular 2 better faster stronger
Finjs - Angular 2 better faster strongerChristoffer Noring
 
006. React - Redux framework
006. React - Redux framework006. React - Redux framework
006. React - Redux frameworkBinh Quan Duc
 
007. Redux middlewares
007. Redux middlewares007. Redux middlewares
007. Redux middlewaresBinh Quan Duc
 
Rethinking Best Practices
Rethinking Best PracticesRethinking Best Practices
Rethinking Best Practicesfloydophone
 
Intellettuali Stranieri a Roma dal Grand Tour al XIX Secolo - 1a parte
Intellettuali Stranieri a Roma dal Grand Tour al XIX Secolo - 1a parteIntellettuali Stranieri a Roma dal Grand Tour al XIX Secolo - 1a parte
Intellettuali Stranieri a Roma dal Grand Tour al XIX Secolo - 1a partePortante Andrea
 
Training lubricants and lubrication wtrpr 2017
Training  lubricants and lubrication wtrpr 2017Training  lubricants and lubrication wtrpr 2017
Training lubricants and lubrication wtrpr 2017M Hussam Adeni
 
Digitalisaation merkitys turvallisuudelle
Digitalisaation merkitys turvallisuudelleDigitalisaation merkitys turvallisuudelle
Digitalisaation merkitys turvallisuudelleJyrki Kasvi
 

Destaque (20)

Rxjs ngvikings
Rxjs ngvikingsRxjs ngvikings
Rxjs ngvikings
 
React + Redux for Web Developers
React + Redux for Web DevelopersReact + Redux for Web Developers
React + Redux for Web Developers
 
Rxjs swetugg
Rxjs swetuggRxjs swetugg
Rxjs swetugg
 
Rxjs ppt
Rxjs pptRxjs ppt
Rxjs ppt
 
React + Redux Introduction
React + Redux IntroductionReact + Redux Introduction
React + Redux Introduction
 
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
 
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
 
Typescript barcelona
Typescript barcelonaTypescript barcelona
Typescript barcelona
 
Angular modules in depth
Angular modules in depthAngular modules in depth
Angular modules in depth
 
Finjs - Angular 2 better faster stronger
Finjs - Angular 2 better faster strongerFinjs - Angular 2 better faster stronger
Finjs - Angular 2 better faster stronger
 
Angular2 rxjs
Angular2 rxjsAngular2 rxjs
Angular2 rxjs
 
Angular2 + rxjs
Angular2 + rxjsAngular2 + rxjs
Angular2 + rxjs
 
React 101
React 101React 101
React 101
 
006. React - Redux framework
006. React - Redux framework006. React - Redux framework
006. React - Redux framework
 
007. Redux middlewares
007. Redux middlewares007. Redux middlewares
007. Redux middlewares
 
Rethinking Best Practices
Rethinking Best PracticesRethinking Best Practices
Rethinking Best Practices
 
Nativescript with angular 2
Nativescript with angular 2Nativescript with angular 2
Nativescript with angular 2
 
Intellettuali Stranieri a Roma dal Grand Tour al XIX Secolo - 1a parte
Intellettuali Stranieri a Roma dal Grand Tour al XIX Secolo - 1a parteIntellettuali Stranieri a Roma dal Grand Tour al XIX Secolo - 1a parte
Intellettuali Stranieri a Roma dal Grand Tour al XIX Secolo - 1a parte
 
Training lubricants and lubrication wtrpr 2017
Training  lubricants and lubrication wtrpr 2017Training  lubricants and lubrication wtrpr 2017
Training lubricants and lubrication wtrpr 2017
 
Digitalisaation merkitys turvallisuudelle
Digitalisaation merkitys turvallisuudelleDigitalisaation merkitys turvallisuudelle
Digitalisaation merkitys turvallisuudelle
 

Semelhante a React lecture

Reactивная тяга
Reactивная тягаReactивная тяга
Reactивная тягаVitebsk Miniq
 
Recompacting your react application
Recompacting your react applicationRecompacting your react application
Recompacting your react applicationGreg Bergé
 
JSLab. Алексей Волков. "React на практике"
JSLab. Алексей Волков. "React на практике"JSLab. Алексей Волков. "React на практике"
JSLab. Алексей Волков. "React на практике"GeeksLab Odessa
 
Intro to React | DreamLab Academy
Intro to React | DreamLab AcademyIntro to React | DreamLab Academy
Intro to React | DreamLab AcademyDreamLab
 
Workshop 20: ReactJS Part II Flux Pattern & Redux
Workshop 20: ReactJS Part II Flux Pattern & ReduxWorkshop 20: ReactJS Part II Flux Pattern & Redux
Workshop 20: ReactJS Part II Flux Pattern & ReduxVisual Engineering
 
React, Redux and es6/7
React, Redux and es6/7React, Redux and es6/7
React, Redux and es6/7Dongho Cho
 
JS Fest 2019. Glenn Reyes. With great power comes great React hooks!
JS Fest 2019. Glenn Reyes. With great power comes great React hooks!JS Fest 2019. Glenn Reyes. With great power comes great React hooks!
JS Fest 2019. Glenn Reyes. With great power comes great React hooks!JSFestUA
 
Intro to Redux | DreamLab Academy #3
Intro to Redux | DreamLab Academy #3 Intro to Redux | DreamLab Academy #3
Intro to Redux | DreamLab Academy #3 DreamLab
 
Manage the Flux of your Web Application: Let's Redux
Manage the Flux of your Web Application: Let's ReduxManage the Flux of your Web Application: Let's Redux
Manage the Flux of your Web Application: Let's ReduxCommit University
 
EWD 3 Training Course Part 39: Building a React.js application with QEWD, Part 3
EWD 3 Training Course Part 39: Building a React.js application with QEWD, Part 3EWD 3 Training Course Part 39: Building a React.js application with QEWD, Part 3
EWD 3 Training Course Part 39: Building a React.js application with QEWD, Part 3Rob Tweed
 
React & Redux for noobs
React & Redux for noobsReact & Redux for noobs
React & Redux for noobs[T]echdencias
 
The evolution of redux action creators
The evolution of redux action creatorsThe evolution of redux action creators
The evolution of redux action creatorsGeorge Bukhanov
 
React + Redux. Best practices
React + Redux.  Best practicesReact + Redux.  Best practices
React + Redux. Best practicesClickky
 
Migrating from Flux to Redux. Why and how.
Migrating from Flux to Redux. Why and how.Migrating from Flux to Redux. Why and how.
Migrating from Flux to Redux. Why and how.Astrails
 
React for Re-use: Creating UI Components with Confluence Connect
React for Re-use: Creating UI Components with Confluence ConnectReact for Re-use: Creating UI Components with Confluence Connect
React for Re-use: Creating UI Components with Confluence ConnectAtlassian
 
Building complex User Interfaces with Sitecore and React
Building complex User Interfaces with Sitecore and ReactBuilding complex User Interfaces with Sitecore and React
Building complex User Interfaces with Sitecore and ReactJonne Kats
 

Semelhante a React lecture (20)

React redux
React reduxReact redux
React redux
 
Reactивная тяга
Reactивная тягаReactивная тяга
Reactивная тяга
 
Recompacting your react application
Recompacting your react applicationRecompacting your react application
Recompacting your react application
 
JSLab. Алексей Волков. "React на практике"
JSLab. Алексей Волков. "React на практике"JSLab. Алексей Волков. "React на практике"
JSLab. Алексей Волков. "React на практике"
 
Intro to React | DreamLab Academy
Intro to React | DreamLab AcademyIntro to React | DreamLab Academy
Intro to React | DreamLab Academy
 
Workshop 20: ReactJS Part II Flux Pattern & Redux
Workshop 20: ReactJS Part II Flux Pattern & ReduxWorkshop 20: ReactJS Part II Flux Pattern & Redux
Workshop 20: ReactJS Part II Flux Pattern & Redux
 
React & Redux
React & ReduxReact & Redux
React & Redux
 
React, Redux and es6/7
React, Redux and es6/7React, Redux and es6/7
React, Redux and es6/7
 
JS Fest 2019. Glenn Reyes. With great power comes great React hooks!
JS Fest 2019. Glenn Reyes. With great power comes great React hooks!JS Fest 2019. Glenn Reyes. With great power comes great React hooks!
JS Fest 2019. Glenn Reyes. With great power comes great React hooks!
 
Intro to Redux | DreamLab Academy #3
Intro to Redux | DreamLab Academy #3 Intro to Redux | DreamLab Academy #3
Intro to Redux | DreamLab Academy #3
 
Manage the Flux of your Web Application: Let's Redux
Manage the Flux of your Web Application: Let's ReduxManage the Flux of your Web Application: Let's Redux
Manage the Flux of your Web Application: Let's Redux
 
EWD 3 Training Course Part 39: Building a React.js application with QEWD, Part 3
EWD 3 Training Course Part 39: Building a React.js application with QEWD, Part 3EWD 3 Training Course Part 39: Building a React.js application with QEWD, Part 3
EWD 3 Training Course Part 39: Building a React.js application with QEWD, Part 3
 
React & Redux for noobs
React & Redux for noobsReact & Redux for noobs
React & Redux for noobs
 
The evolution of redux action creators
The evolution of redux action creatorsThe evolution of redux action creators
The evolution of redux action creators
 
React + Redux. Best practices
React + Redux.  Best practicesReact + Redux.  Best practices
React + Redux. Best practices
 
Migrating from Flux to Redux. Why and how.
Migrating from Flux to Redux. Why and how.Migrating from Flux to Redux. Why and how.
Migrating from Flux to Redux. Why and how.
 
React for Re-use: Creating UI Components with Confluence Connect
React for Re-use: Creating UI Components with Confluence ConnectReact for Re-use: Creating UI Components with Confluence Connect
React for Re-use: Creating UI Components with Confluence Connect
 
React outbox
React outboxReact outbox
React outbox
 
Redux vs Alt
Redux vs AltRedux vs Alt
Redux vs Alt
 
Building complex User Interfaces with Sitecore and React
Building complex User Interfaces with Sitecore and ReactBuilding complex User Interfaces with Sitecore and React
Building complex User Interfaces with Sitecore and React
 

Mais de Christoffer Noring (20)

Azure signalR
Azure signalRAzure signalR
Azure signalR
 
Game dev 101 part 3
Game dev 101 part 3Game dev 101 part 3
Game dev 101 part 3
 
Game dev 101 part 2
Game dev 101   part 2Game dev 101   part 2
Game dev 101 part 2
 
Game dev workshop
Game dev workshopGame dev workshop
Game dev workshop
 
Deploying your static web app to the Cloud
Deploying your static web app to the CloudDeploying your static web app to the Cloud
Deploying your static web app to the Cloud
 
IaaS with ARM templates for Azure
IaaS with ARM templates for AzureIaaS with ARM templates for Azure
IaaS with ARM templates for Azure
 
Learning Svelte
Learning SvelteLearning Svelte
Learning Svelte
 
Ng spain
Ng spainNg spain
Ng spain
 
Angular Schematics
Angular SchematicsAngular Schematics
Angular Schematics
 
Design thinking
Design thinkingDesign thinking
Design thinking
 
Keynote ijs
Keynote ijsKeynote ijs
Keynote ijs
 
Vue fundamentasl with Testing and Vuex
Vue fundamentasl with Testing and VuexVue fundamentasl with Testing and Vuex
Vue fundamentasl with Testing and Vuex
 
Ngrx slides
Ngrx slidesNgrx slides
Ngrx slides
 
Kendoui
KendouiKendoui
Kendoui
 
Angular mix chrisnoring
Angular mix chrisnoringAngular mix chrisnoring
Angular mix chrisnoring
 
Nativescript angular
Nativescript angularNativescript angular
Nativescript angular
 
Graphql, REST and Apollo
Graphql, REST and ApolloGraphql, REST and Apollo
Graphql, REST and Apollo
 
Angular 2 introduction
Angular 2 introductionAngular 2 introduction
Angular 2 introduction
 
Rxjs vienna
Rxjs viennaRxjs vienna
Rxjs vienna
 
Rxjs marble-testing
Rxjs marble-testingRxjs marble-testing
Rxjs marble-testing
 

Último

TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 

Último (20)

TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 

React lecture

  • 1. React & Redux Christoffer Noring Google Developer Expert
  • 2. Contents React basics Flux Redux CSS Mixins Router Tooling and Best Practices covered by Fabrice and Mathieu
  • 5. React DOM ReactDOM.render( [Component]/ [JSX Expression], [element] ) Render our app
  • 6. Minimum Setup ES5 <script src="node_modules/react/dist/react.js"></script> <script src="node_modules/react-dom/dist/react-dom.js"></script> <script src=“https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.34/browser.min.js"> script> <body> <div id="example"></div> <script type="text/babel"> ReactDOM.render( ); </script> </body> <h1>Hello, world!</h1>, document.getElementById('example') Where to render the appContent
  • 7. JSX Syntax extension to javascript const element = <h1>Hello, world!</h1>; Allows us to do things like: Needs to be transpiled : const element = ( <h1 className="greeting"> Hello, world! </h1> ); JSX const element = React.createElement( 'h1', {className: 'greeting'}, 'Hello, world!' ); ES5 JSX is your friend, you don’t have to use it HTML in my javascript!!
  • 9. Your first component app.js index.html var App = React.createClass({ render: function() { return ( <div className="app"> Hello, world! I am an app component </div> ); } }); ReactDOM.render( <App />, document.getElementById('example') ); render() is like a paint method <body> <div id="example"></div> <script type="text/babel"> </script> </body> <h1>Hello, world!</h1>, document.getElementById('examp ReactDOM.render( ); <script type="text/babel" src="app.js"></script> replace with createClass(), creates the component
  • 10. Your second component var CV = React.createClass({ render: function() { return ( <div className="comments"> Showing your cv </div> ); } }); var App = React.createClass({ render: function() { return ( <CV /> ); } }); Component within a component = component centric programming Call createClass() again return CV component from App component
  • 11. Component, input <Component prop={ data }> this.props.prop
  • 12. Component with data let data = { name : 'chris', profession : 'software developer', skills : ['.net', 'javascript', 'angular', 'react'] } This is what we want to render looks like a property looks like a list ReactDOM.render( <App data={ data } />, document.getElementById('example') ); property = { variable } Access data inside component like this: var App = React.createClass({ render: function() { return ( <div> { this.props.data.name } </div> ); } }); this.props.data.name “chris”
  • 13. Rendering a list var App = React.createClass({ render: function() { return ( <div> <div> { this.props.data.name } </div> <div>{ skills }</div> </div> ); } }); let skills = this.props.data.skills.map(skill => { return ( <div className="skill">{skill}</div> ); }); Projection Interpolation Everything is in the “App” component = BAD
  • 14. Refactoring Putting everything in the App component isn’t very “react like” of us var App = React.createClass({ render: function() { return ( <CV data={ this.props.data } /> ); } }); var CV = React.createClass({ render: function() { return ( <div className="cv"> { this.data.props.name } <Skills data={ this.data.props.skills } > </div> ); } }); var Skills = React.createClass({ render : function(){ var skills = this.props.data.map(function(skill) { return ( <div className="skill">{skill}</div> ); }); return( <div className="skills"> <h2>Skills</h2> { skills } </div> ); } })
  • 15. We can do this even better var Skills = React.createClass({ render : function(){ var skills = this.props.data.map(function(skill) { return ( <Skill data={ skill } /> ); }); return( <div className="skills"> <h2>Skills</h2> { skills } </div> ); } }) var Skill = React.createClass({ render : function(){ return( <div className="skills"> <h3>{ this.props.data }</h3> </div> ); } }) A component should do one thing well One Skill comp per skill item
  • 16. Component tree so far App CV Skills Skill Skill Skill etc…
  • 18. <button type="submit" onClick={this.onSubmit} > Save new skills </button> var CV = React.createClass({ onSubmit : function(e){ e.preventDefault(); // do stuff }, render : function(){ return ( ) } Add method to our object literal Refer to method in markup
  • 19. State
  • 20. Component State var App = React.createClass({ getInitialState : function() { return { a : ‘some state’ }; }, render : function() { return <div>{ this.state.a }</div> } }); Reading state getInitialState is read once per bootstrap, define your state here this.state.<prop>
  • 21. Changing state var App = React.createClass({ getInitialState : function() { return { newSkill : ‘some state’, b : ‘some other state’ }; }, render : function() { return <div> <input value={this.state.newSkill} > { this.state.a } </div> } For every change of input field update state onChange={this.onSkillChange} bind to onchange onSkillChange : function(e){ this.setState({newSkill : e.target.value}); }
  • 22. State summary getInitialState(){ return { prop : ‘’ } } { this.state.prop } this.setState({ prop : ‘newValue’ })
  • 23. Lifecycle events Initial State Changes Props changes Unmounting
  • 25. State changes shouldComponentUpdate componentWillUpdate render componentDidUpdate boolean : is rerendering needed prepare for update perform DOM operations
  • 27. Unmounting componentWillUnmount is called before component is removed from DOM do cleanup, think of it as a destructor
  • 28. ES6
  • 29. We want to write in ES6 cause it has nice features Object.assign() Let Const Spread Operator Lambdas ES6 modules import {} from ‘’ export default export { a,b,c }
  • 30. ES6 component class Todo extends React.Component { constructor(){ super(); this.action = this.action.bind(this); this.state.prop = value } render() { return ( <div onClick={this.action}>{this.props.title}</div> ) } action(e){ this.props.click( this.props.id ); } } GOTCHA, we need to call the following, for our methods to be picked up We inherit from React.Component instead of calling React.createClass({}) Otherwise its business as usual we DON’t use getInitialState(){} we just set this.state in constructor We create a proper class rather than an object literal
  • 32. Catch bugs with type checking Will give an error in a tool, wrong type Component.propTypes ={ title : React.PropTypes.string } <Component title=1 > Example 1 - wrong type Component.propTypes ={ title : React.PropTypes.string.isRequired } Example 2 - required <Component > Will giver error, prop missing
  • 33. Many validation types oneOfType oneOf arrayOf objectOf element instanceOf symbol custom Further reading, https://facebook.github.io/react/docs/ typechecking-with-proptypes.html
  • 35. Action describes what should happen with what data Action Dispatcher Store React e.g. Add todo Store notifies listener that data has changed and needs to be reread Dispatch action to store, tell the store Unidirectional flow, everything flows in one direction
  • 36. Action Dispatcher Store React { type : ADD_TODO, todo : { title : ‘dfdfd’ } } What Payload
  • 37. Dispatcher Action Dispatcher Store React Dispatches actions dispatch(action) Handles a dispatched action through register(function(action){})
  • 38. Store Action Dispatcher Store React Contains your state, usually same file have ability to perform ajax and also notify listeners when state has been updated
  • 39. var Todos = React.createClass({ getInitialState : function(){ return { todos : Store.getTodos() } } render : function(){ return ([ render todos ]) }, }) //todos-component.js componentDidMount : function(){ Store.addChangeListener( this._onChange ) }, componentWillUnmount : function(){ Store.removeChangeListener( this._onChange ) }, onChange() { this.setState({ todos : Store.getTodos() }) } Get data 1 Update state so render() is called 2
  • 40. store.js var todos = []; function loadTodos(){ return todos; } var Store = merge(EventEmitter.prototype, { getTodos : function(){ return todos; } emitChange : function(){ emit(‘change’) }, addChangeListener : function(callback){ this.on(‘change’, callback) }, removeChangeListener : function(callback) { this.removeListener(‘change’, callback) } }) Dispatcher.register(function(payload){ var action = payload.action; switch(action) { case ADD_TODO: todos.push( payload.data ) case LOAD_TODOS: loadTodos(); } Store.emitChange(); }) Calls _onChange() on the component Called when Dispatcher.dispatch() getData addListener removeListener notifyChange
  • 41. var AddTodo = React.createClass({ render : function(){ return ([ todo input ]) }, createTodo : function(todo){ Actions.addTodo( todo ) } }) //add-todo-component.js Calls the ActionCreator with a type and a payload
  • 42. ActionCreator Actions = { addTodo : function(todo) { Dispatcher.dispatch( { actionType : ADD_TODO, data : todo } ) } … } Dispatch the action
  • 43. Redux
  • 44. Why Redux or Flux growing pains Scales well on larger complex apps Boiler plate is way too big on smaller apps
  • 47. Action Store React Reducers function addTodo( todo ) { return { type : ‘ADD_TODO’, todo: todo } } Action Creator Represent user intent Must have a type
  • 49. store.dispatch( action ); store.subscribe( listener ); store.getState() replaceReducer( nextReducer ) Action Store React Reducers
  • 51. Action Store React Reducers function reducer(state, action) { return newState; } (state, action) => state Must be pure Multiple reducers per app We want to change the state, without mutating so how?
  • 53. Change Object What about a large object hierarchy? Object.assign( target, oldObject, newState ) Object.assign( {}, { update : false, name : ‘’ }, { update : true } ) { update : true, name : ‘’ } Merged
  • 54. Changing array //mutable state = [ { id : 1, name : 'tomato' } ] state.push( { id: 2, name : 'cucumber' } ); return state; Mutating Point of mutation //immutable state = [ { id : 1, name : 'tomato' } ] return [ ...state, Object.assign({}, state, course) ] Immutable old array + new item Spread operator …
  • 55. Reducer no-no list Mutate arguments Perform side effects Call non-pure functions
  • 56. Benefits to immutable state Clarity answers - who changed that state? Mutable, anyone could have changed it Immutable, only a reducer could have changed it Performance No need to check every single property on an object if(oldState != newState), reference check Time travel debugging
  • 57. State Summary ES5 lodash merge lodash extend Object-assign ( NPM ) Reference checking is super fast Enables time-travel debugging through browser plugin Object.assign() … spread operator for arrays ES6 Change state by
  • 58. All Reducers are called on each dispatch addItemReducer removeItemReducer listItemsReducer Dispatching addItem { type: ADD_ITEM, : item : item } returns new state return state return state
  • 59. Connect Redux to our App
  • 60. react-redux Action Store React Reducers Provider, attaches app to store Connect, creates container items
  • 61. Connect store data to our app ComponentApp Provider Store Data Setup our store Wrap App component in a Provider
  • 62. import { combineReducers } from 'redux'; import todos from './todo-reducer'; const rootReducer = combineReducers({ todos : todos }) // OR todos only, LHS is implied export default rootReducer; reducers/index.js Root reducer Combines all reducers in one, this is one we feed to the store
  • 63. Initialise store import { createStore, applyMiddleware } from 'redux'; import rootReducer from '../reducers' // index.js import reduxImmutableStateInvariant from 'redux-immutable-state-invariant'; export default function configureStore(initialState) { return createStore( rootReducer, initialState, applyMiddleware( reduxImmutableStateInvariant() ) ) } configure-store.js Create store give it a root reducer initial state and add middleware
  • 64. Provider <Provider store={store} > <App /> </Provider> Uses Reacts context - don’t touch import configureStore from './store/configureStore'; import { Provider } from 'ReactRedux'; Make your store available to all your components
  • 65. Component Wrap our component in a container component using Connect Presentational Component Container component export default connect( mapStateToProps, mapDispatchToProps )( PresentationalComponent ) what state what functions
  • 66. what state should I expose as props mapStateToProps mapStateToProps = () => { return { appState : ‘’, otherAppState : ‘’ } } // Component this.props.appState this.props.otherAppState Every time a change happens this function is rerun, so don’t do anything expensive in there
  • 67. mapDispatchToProps cont.. mapDispatchToProps = (dispatch) { return { loadTodos : () => { dispatch( loadTodos() ); }, addTodo : (todo) => { dispatch( addTodo(todo) ); } } } Manual approach, but clear whats happening, recommended when starting out e.g. this.props.loadTodos();
  • 68. mapDispatchToProps - nicer what actions do we want to expose to the component mapDispatchToProps = () => { return { actions : bindActionCreators(actions, dispatch) } } This is a shorthand using Redux this.actions.methodName()
  • 69. Container component Smart component Focus on how things work “Aware” of Redux Subscribe to Redux state Dispatch redux actions react-redux lib
  • 70. Presentational component Normal component that show data through props, knows nothing about Redux Presentation component gets decorated
  • 71. class TodoComponent extends React.Component{ onSave(){ this.props.dispatch(todoActions.addTodo(this.state.todo)); //console.log(this.state.todo.title); } } function mapStateToProps(state, ownProps) { return { todos : state.todos } } export default connect( mapStateToProps) (TodoComponent); 1 2 3 Reduxify your component in 3 steps Decorate our Component Expose state Calling and dispatching an action
  • 72. Redux flow so far Actions Dispatches an action Reducers current state + action = new state Store let connected components know of state change ReactRedux determine wether to tell React to update UI React new data passed through props YES Something happenedReact
  • 73. Redux concepts so far Container and Presentation components Has methods, Has state, Knows Redux Just props No Redux ReactRedux Provider Pass store data to Components Connect mapStateToProps Pass state and methods to Components mapDispatchToProps
  • 76. Change our call to .connect() export default connect( mapStateToProps) (TodoComponent); export default connect( mapStateToProps mapDispatchToProps) (TodoComponent); function mapDispatchToProps(dispatch) { return addTodo : todo => dispatch(todoActions.addTodo(todo)) } Wrap the action in a dispatch()
  • 77. onSave(){ this.props.dispatch(todoActions.addTodo(this.state.todo)); } Change to onSave(){ this.props.addTodo(this.state.todo)); } All knowledge of dispatch is removed Much cleaner Dispatch() is no longer injected into the component Fixing the component
  • 78. And a little more…
  • 79. import { bindActionCreators } from ‘redux’ This function mapDispatchToProps(dispatch) { return addTodo : todo => dispatch(todoActions.addTodo(todo)) } Becomes this function mapDispatchToProps(dispatch) { return actions : bindActionCreators( todoActions, dispatch ) } MAGIC !! onSave(){ this.props.actions.addTodo(this.state.todo)); }
  • 80. Clean up magic strings
  • 81. export default { ADD_TODO : 'ADD_TODO' } actionTypes.js todo-actions.js import actionTypes from './action-types'; function addTodo(todo) { return { type : actionTypes.ADD_TODO, todo : todo }; } todo-reducer.js export default function todoReducer(state = [], action) { switch(action.type) { case actionTypes.ADD_TODO: return [ ...state, Object.assign({}, action.todo) ] And so on… Replace string with constant
  • 83. export function loadTodos() { return function(dispatch) { return service.getTodos().then( todos => { dispatch( loadTodosSuccess(todos) ) }).catch(error => { console.error(error) }) } } export function loadTodosSuccess(todos) { return { type : types.LOAD_TODOS, todos : todos } } todo-actions.js index.js const store = configureStore(); store.dispatch( loadTodos() ) set initial data Call dispatch when Ajax is done
  • 84. export default function todoReducer( state = [], action) { switch(action.type) { … case actionTypes.LOAD_TODOS : return action.todos default : return state; } } todo-reducer.js
  • 85. Component class TodoComponent extends React.Component{ render(){ let todos = this.state.todos.map( todo => { return <div>{ todo }</div> }) return ( <div>{ todos }</div> ) } } function mapStateToProps(state, ownProps) { return { todos : state.todos } } export default connect( mapStateToProps) (TodoComponent);
  • 86. Async load summary Create async action that calls dispatch when done Add reducer for loading data Add said reducer to rootReducer store.dispatch( loadAction() ) in index.js ensure its exposed in mapStateToProps use in presentation component