SlideShare una empresa de Scribd logo
1 de 26
Descargar para leer sin conexión
BY MICHEL PEREZ
REACT + REDUX
REACT
React is a JavaScript Library for building user interfaces.
• Focus on the UI, not a Framework like Angular or Ember
• One-way reactive data flow (no two-way data binding)
• Components
• Virtual DOM
VIRTUAL DOM
Keep track of state in DOM is hard
The DOM API is slow.
Efficient updates to subtrees
VIRTUAL DOM UPDATING
THE HELLO WORLD
import React from 'react'

import ReactDOM from 'react-dom'



const exampleElement = document.getElementById('example')

ReactDOM.render(<h1>Hello, world!</h1>, exampleElement)
<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8" />

<script src="bundle.js"></script>

</head>

<body>

<div id="example"></div>

</body>

</html>
JSX
JSX is a JavaScript syntax extension that looks similar to
XML is used to create React Elements
// Input (JSX):

var app = <h2 color="blue" />

// Output (JS):

var app = React.createElement(h2, {color:"blue"})
STATEFUL COMPONENT
class DeleteLink extends React.Component{

constructor(props){

super()
this.state = {}

this.index = props.index

this.deleteComponentHandler = this.deleteComponentHandler.bind(this) 

}



render(){

return(

<a href="#" className="delete-component" onClick={(e) => {

e.preventDefault()

this.deleteComponentHandler(index)

}}>

<i className="fa fa-trash" aria-hidden="true"></i>

</a>

)

}



deleteComponentHandler(index){

// do something

}

}



export default DeleteLink
COMPONENT LIFECYCLE
class MyComponent extends React.Component {

constructor() {

}

render() { //pure returns the view calling jsx tags

}

getInitialState() { // called before the component is mounted, inits this.state

}

getDefaultProps() { // inits this.props

}

componentWillMount() { called once before the initial render

}

componentDidMount() { // called once after the initial render

}

componentWillReceiveProps() { // called when the props are updated

}

shouldComponentUpdate() { // should update the component in the three?

}

componentWillUpdate() { // do something before an update, not to call
this.setState

}

componentDidUpdate() { // do something when the components have been updated

}

}
STATELESS COMPONENT
import React, {PropTypes} from 'react'



const DeleteLink = ({index}) => {

return (

<a href="#" className="delete-component">

<i className="fa fa-trash" aria-hidden="true"></i>

</a>

)

}



DeleteLink.propTypes = {

index: PropTypes.number.isRequired,

deleteComponentHandler: PropTypes.func.isRequired

}



export default DeleteLink

WHY STATELESS?
▸ Predictable
▸ Functional Programming
▸ Easy to understand
▸ Easy to test
REACT + ?
REDUX
▸ Handles the state of the Application with predictable
behaviour
▸ Takes ideas from ELM
▸ Store: The state
▸ Actions: The intent of changing the state
▸ Reducer: (previousState, action) => newState
▸ Components: The view (Here goes react)
▸ Containers: Components decorators
REDUX ARCHITECTURE
Store
Action
Creators
Reducers
dispatch(action)
(newState)
(state)
(previousState, action)
React
Components
STORE
import React from 'react'

import { render } from 'react-dom'

import { Provider } from 'react-redux'

import { createStore } from 'redux'

import todoApp from './reducers'

import App from './components/App'



let store = createStore(todoApp)



render(

<Provider store={store}>

<App />

</Provider>,

document.getElementById('main')

)
THE APP
import React from 'react'

import AddTodoContainer from '../containers/AddTodoContainer'

import VisibleTodoList from '../containers/VisibleTodoList'



const App = () => (

<div>

<AddTodoContainer />

<VisibleTodoList />

</div>

)



export default App
THE ADD TODO
import React, { PropTypes } from 'react'



let AddTodo = ({ onInputSubmit }) => {

let input



return (

<div>

<form onSubmit={(e) => onInputSubmit(e, input)}>

<input ref={node => input = node } />

<button type="submit">

Add Todo

</button>

</form>

</div>

)

}



AddTodo.propTypes = {

onInputSubmit: PropTypes.func.isRequired

}



export default AddTodo
THE CONTAINER
import React from 'react'

import { connect } from 'react-redux'

import { addTodo } from '../actions'

import AddTodo from '../components/AddTodo'



const mapDispatchToProps = (dispatch) => {

return {

onInputSubmit: (e, input) => {

e.preventDefault()

if (!input.value.trim()) {

return

}

dispatch(addTodo(input.value))

input.value = ''

}

} 

}



const AddTodoContainer = connect(null, mapDispatchToProps)(AddTodo)



export default AddTodoContainer
THE ACTIONS
let nextTodoId = 0


export const addTodo = (text) => {

return {

type: 'ADD_TODO',

id: nextTodoId++,

text

}

}
THE REDUCER
import { combineReducers } from 'redux'

import todos from './todos'



const todoApp = combineReducers({

todos

})

const todos = (state = [], action) => {

switch (action.type) {

case 'ADD_TODO':

return [

…state,
{id: action.id, text: action.text, completed: false}

]

default:

return state

}

}

export default todoApp
THE TODO LIST
import React, { PropTypes } from 'react'

import Todo from './Todo'



const TodoList = ({ todos }) => (

<ul>

{todos.map(todo =>

<Todo

key={todo.id}

{...todo}

/>

)}

</ul>

)



TodoList.propTypes = {

todos: PropTypes.arrayOf(PropTypes.shape({

id: PropTypes.number.isRequired,

completed: PropTypes.bool.isRequired,

text: PropTypes.string.isRequired

}).isRequired).isRequired

}



export default TodoList
THE TODO ITEM
import React, { PropTypes } from 'react'



const Todo = ({ completed, text }) => (

<li>

{text}

</li>

)



Todo.propTypes = {

completed: PropTypes.bool.isRequired,

text: PropTypes.string.isRequired

}



export default Todo
THE TODO LIST CONTAINER
import { connect } from 'react-redux'

import TodoList from '../components/TodoList'



const mapStateToProps = (state) => {

return {

todos: state.todos

}

}



const mapDispatchToProps = (dispatch) => {

return { }

}



const VisibleTodoList = connect(

mapStateToProps,

mapDispatchToProps

)(TodoList)



export default VisibleTodoList
ES6 THE IMMUTABLE
A new array
[

…state,
{id: action.id, text: action.text, completed: true}

]
A new object
return {...image, _destroy: {value: true}}
Immutable.js
https://facebook.github.io/immutable-js/
CONCLUSIONS
CONCLUSIONS II
THANKS

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

React.js and Redux overview
React.js and Redux overviewReact.js and Redux overview
React.js and Redux overview
 
React / Redux Architectures
React / Redux ArchitecturesReact / Redux Architectures
React / Redux Architectures
 
React и redux
React и reduxReact и redux
React и redux
 
Switch to React.js from AngularJS developer
Switch to React.js from AngularJS developerSwitch to React.js from AngularJS developer
Switch to React.js from AngularJS developer
 
Introduction to react and redux
Introduction to react and reduxIntroduction to react and redux
Introduction to react and redux
 
State Models for React with Redux
State Models for React with ReduxState Models for React with Redux
State Models for React with Redux
 
Designing applications with Redux
Designing applications with ReduxDesigning applications with Redux
Designing applications with Redux
 
React & redux
React & reduxReact & redux
React & redux
 
Intro to Redux | DreamLab Academy #3
Intro to Redux | DreamLab Academy #3 Intro to Redux | DreamLab Academy #3
Intro to Redux | DreamLab Academy #3
 
React + Redux. Best practices
React + Redux.  Best practicesReact + Redux.  Best practices
React + Redux. Best practices
 
Building React Applications with Redux
Building React Applications with ReduxBuilding React Applications with Redux
Building React Applications with Redux
 
React + Redux Introduction
React + Redux IntroductionReact + Redux Introduction
React + Redux Introduction
 
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
 
ProvJS: Six Months of ReactJS and Redux
ProvJS:  Six Months of ReactJS and ReduxProvJS:  Six Months of ReactJS and Redux
ProvJS: Six Months of ReactJS and Redux
 
Redux js
Redux jsRedux js
Redux js
 
Better React state management with Redux
Better React state management with ReduxBetter React state management with Redux
Better React state management with Redux
 
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
 
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
 
ReactJs presentation
ReactJs presentationReactJs presentation
ReactJs presentation
 

Destacado

Destacado (18)

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
 
GraphQL With Relay Part Deux
GraphQL With Relay Part DeuxGraphQL With Relay Part Deux
GraphQL With Relay Part Deux
 
Collaborative music with elm and phoenix
Collaborative music with elm and phoenixCollaborative music with elm and phoenix
Collaborative music with elm and phoenix
 
Workshop 22: React-Redux Middleware
Workshop 22: React-Redux MiddlewareWorkshop 22: React-Redux Middleware
Workshop 22: React-Redux Middleware
 
From Hacker to Programmer (w/ Webpack, Babel and React)
From Hacker to Programmer (w/ Webpack, Babel and React)From Hacker to Programmer (w/ Webpack, Babel and React)
From Hacker to Programmer (w/ Webpack, Babel and React)
 
Nodejs - A quick tour (v6)
Nodejs - A quick tour (v6)Nodejs - A quick tour (v6)
Nodejs - A quick tour (v6)
 
DotNet MVC and webpack + Babel + react
DotNet MVC and webpack + Babel + reactDotNet MVC and webpack + Babel + react
DotNet MVC and webpack + Babel + react
 
bluespec talk
bluespec talkbluespec talk
bluespec talk
 
Rethink Frontend Development With Elm
Rethink Frontend Development With ElmRethink Frontend Development With Elm
Rethink Frontend Development With Elm
 
Webpack & React Performance in 16+ Steps
Webpack & React Performance in 16+ StepsWebpack & React Performance in 16+ Steps
Webpack & React Performance in 16+ Steps
 
Webpack slides
Webpack slidesWebpack slides
Webpack slides
 
Packing for the Web with Webpack
Packing for the Web with WebpackPacking for the Web with Webpack
Packing for the Web with Webpack
 
Introduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScriptIntroduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScript
 
Rethinking Best Practices
Rethinking Best PracticesRethinking Best Practices
Rethinking Best Practices
 
Introduction to Elm
Introduction to ElmIntroduction to Elm
Introduction to Elm
 
React js
React jsReact js
React js
 
Redux, Relay, HorizonあるいはElm
Redux, Relay, HorizonあるいはElmRedux, Relay, HorizonあるいはElm
Redux, Relay, HorizonあるいはElm
 
Elm intro
Elm introElm intro
Elm intro
 

Similar a React redux

Reactive.architecture.with.Angular
Reactive.architecture.with.AngularReactive.architecture.with.Angular
Reactive.architecture.with.Angular
Evan Schultz
 

Similar a React redux (20)

React lecture
React lectureReact lecture
React lecture
 
Intro to React | DreamLab Academy
Intro to React | DreamLab AcademyIntro to React | DreamLab Academy
Intro to React | DreamLab Academy
 
The evolution of redux action creators
The evolution of redux action creatorsThe evolution of redux action creators
The evolution of redux action creators
 
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 outbox
React outboxReact outbox
React outbox
 
Reactивная тяга
Reactивная тягаReactивная тяга
Reactивная тяга
 
React hooks
React hooksReact hooks
React hooks
 
Introducing Vuex in your project
Introducing Vuex in your projectIntroducing Vuex in your project
Introducing Vuex in your project
 
State manager in Vue.js, from zero to Vuex
State manager in Vue.js, from zero to VuexState manager in Vue.js, from zero to Vuex
State manager in Vue.js, from zero to Vuex
 
Reactive.architecture.with.Angular
Reactive.architecture.with.AngularReactive.architecture.with.Angular
Reactive.architecture.with.Angular
 
React & Redux for noobs
React & Redux for noobsReact & Redux for noobs
React & Redux for noobs
 
Side effects-con-redux
Side effects-con-reduxSide effects-con-redux
Side effects-con-redux
 
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
 
Universal JS Web Applications with React - Web Summer Camp 2017, Rovinj (Work...
Universal JS Web Applications with React - Web Summer Camp 2017, Rovinj (Work...Universal JS Web Applications with React - Web Summer Camp 2017, Rovinj (Work...
Universal JS Web Applications with React - Web Summer Camp 2017, Rovinj (Work...
 
Integrating React.js with PHP projects
Integrating React.js with PHP projectsIntegrating React.js with PHP projects
Integrating React.js with PHP projects
 
Recompacting your react application
Recompacting your react applicationRecompacting your react application
Recompacting your react application
 
Higher Order Components and Render Props
Higher Order Components and Render PropsHigher Order Components and Render Props
Higher Order Components and Render Props
 
React, Redux and es6/7
React, Redux and es6/7React, Redux and es6/7
React, Redux and es6/7
 
React/Redux
React/ReduxReact/Redux
React/Redux
 
2018 05-16 Evolving Technologies: React, Babel & Webpack
2018 05-16 Evolving Technologies: React, Babel & Webpack2018 05-16 Evolving Technologies: React, Babel & Webpack
2018 05-16 Evolving Technologies: React, Babel & Webpack
 

Último

%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
chiefasafspells
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
masabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 

Último (20)

%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 

React redux

  • 2. REACT React is a JavaScript Library for building user interfaces. • Focus on the UI, not a Framework like Angular or Ember • One-way reactive data flow (no two-way data binding) • Components • Virtual DOM
  • 3. VIRTUAL DOM Keep track of state in DOM is hard The DOM API is slow. Efficient updates to subtrees
  • 5. THE HELLO WORLD import React from 'react'
 import ReactDOM from 'react-dom'
 
 const exampleElement = document.getElementById('example')
 ReactDOM.render(<h1>Hello, world!</h1>, exampleElement) <!DOCTYPE html>
 <html>
 <head>
 <meta charset="UTF-8" />
 <script src="bundle.js"></script>
 </head>
 <body>
 <div id="example"></div>
 </body>
 </html>
  • 6. JSX JSX is a JavaScript syntax extension that looks similar to XML is used to create React Elements // Input (JSX):
 var app = <h2 color="blue" />
 // Output (JS):
 var app = React.createElement(h2, {color:"blue"})
  • 7. STATEFUL COMPONENT class DeleteLink extends React.Component{
 constructor(props){
 super() this.state = {}
 this.index = props.index
 this.deleteComponentHandler = this.deleteComponentHandler.bind(this) 
 }
 
 render(){
 return(
 <a href="#" className="delete-component" onClick={(e) => {
 e.preventDefault()
 this.deleteComponentHandler(index)
 }}>
 <i className="fa fa-trash" aria-hidden="true"></i>
 </a>
 )
 }
 
 deleteComponentHandler(index){
 // do something
 }
 }
 
 export default DeleteLink
  • 8. COMPONENT LIFECYCLE class MyComponent extends React.Component {
 constructor() {
 }
 render() { //pure returns the view calling jsx tags
 }
 getInitialState() { // called before the component is mounted, inits this.state
 }
 getDefaultProps() { // inits this.props
 }
 componentWillMount() { called once before the initial render
 }
 componentDidMount() { // called once after the initial render
 }
 componentWillReceiveProps() { // called when the props are updated
 }
 shouldComponentUpdate() { // should update the component in the three?
 }
 componentWillUpdate() { // do something before an update, not to call this.setState
 }
 componentDidUpdate() { // do something when the components have been updated
 }
 }
  • 9. STATELESS COMPONENT import React, {PropTypes} from 'react'
 
 const DeleteLink = ({index}) => {
 return (
 <a href="#" className="delete-component">
 <i className="fa fa-trash" aria-hidden="true"></i>
 </a>
 )
 }
 
 DeleteLink.propTypes = {
 index: PropTypes.number.isRequired,
 deleteComponentHandler: PropTypes.func.isRequired
 }
 
 export default DeleteLink

  • 10. WHY STATELESS? ▸ Predictable ▸ Functional Programming ▸ Easy to understand ▸ Easy to test
  • 12. REDUX ▸ Handles the state of the Application with predictable behaviour ▸ Takes ideas from ELM ▸ Store: The state ▸ Actions: The intent of changing the state ▸ Reducer: (previousState, action) => newState ▸ Components: The view (Here goes react) ▸ Containers: Components decorators
  • 14. STORE import React from 'react'
 import { render } from 'react-dom'
 import { Provider } from 'react-redux'
 import { createStore } from 'redux'
 import todoApp from './reducers'
 import App from './components/App'
 
 let store = createStore(todoApp)
 
 render(
 <Provider store={store}>
 <App />
 </Provider>,
 document.getElementById('main')
 )
  • 15. THE APP import React from 'react'
 import AddTodoContainer from '../containers/AddTodoContainer'
 import VisibleTodoList from '../containers/VisibleTodoList'
 
 const App = () => (
 <div>
 <AddTodoContainer />
 <VisibleTodoList />
 </div>
 )
 
 export default App
  • 16. THE ADD TODO import React, { PropTypes } from 'react'
 
 let AddTodo = ({ onInputSubmit }) => {
 let input
 
 return (
 <div>
 <form onSubmit={(e) => onInputSubmit(e, input)}>
 <input ref={node => input = node } />
 <button type="submit">
 Add Todo
 </button>
 </form>
 </div>
 )
 }
 
 AddTodo.propTypes = {
 onInputSubmit: PropTypes.func.isRequired
 }
 
 export default AddTodo
  • 17. THE CONTAINER import React from 'react'
 import { connect } from 'react-redux'
 import { addTodo } from '../actions'
 import AddTodo from '../components/AddTodo'
 
 const mapDispatchToProps = (dispatch) => {
 return {
 onInputSubmit: (e, input) => {
 e.preventDefault()
 if (!input.value.trim()) {
 return
 }
 dispatch(addTodo(input.value))
 input.value = ''
 }
 } 
 }
 
 const AddTodoContainer = connect(null, mapDispatchToProps)(AddTodo)
 
 export default AddTodoContainer
  • 18. THE ACTIONS let nextTodoId = 0 
 export const addTodo = (text) => {
 return {
 type: 'ADD_TODO',
 id: nextTodoId++,
 text
 }
 }
  • 19. THE REDUCER import { combineReducers } from 'redux'
 import todos from './todos'
 
 const todoApp = combineReducers({
 todos
 })
 const todos = (state = [], action) => {
 switch (action.type) {
 case 'ADD_TODO':
 return [
 …state, {id: action.id, text: action.text, completed: false}
 ]
 default:
 return state
 }
 }
 export default todoApp
  • 20. THE TODO LIST import React, { PropTypes } from 'react'
 import Todo from './Todo'
 
 const TodoList = ({ todos }) => (
 <ul>
 {todos.map(todo =>
 <Todo
 key={todo.id}
 {...todo}
 />
 )}
 </ul>
 )
 
 TodoList.propTypes = {
 todos: PropTypes.arrayOf(PropTypes.shape({
 id: PropTypes.number.isRequired,
 completed: PropTypes.bool.isRequired,
 text: PropTypes.string.isRequired
 }).isRequired).isRequired
 }
 
 export default TodoList
  • 21. THE TODO ITEM import React, { PropTypes } from 'react'
 
 const Todo = ({ completed, text }) => (
 <li>
 {text}
 </li>
 )
 
 Todo.propTypes = {
 completed: PropTypes.bool.isRequired,
 text: PropTypes.string.isRequired
 }
 
 export default Todo
  • 22. THE TODO LIST CONTAINER import { connect } from 'react-redux'
 import TodoList from '../components/TodoList'
 
 const mapStateToProps = (state) => {
 return {
 todos: state.todos
 }
 }
 
 const mapDispatchToProps = (dispatch) => {
 return { }
 }
 
 const VisibleTodoList = connect(
 mapStateToProps,
 mapDispatchToProps
 )(TodoList)
 
 export default VisibleTodoList
  • 23. ES6 THE IMMUTABLE A new array [
 …state, {id: action.id, text: action.text, completed: true}
 ] A new object return {...image, _destroy: {value: true}} Immutable.js https://facebook.github.io/immutable-js/