SlideShare a Scribd company logo
1 of 57
Download to read offline
React Native Munich Meetup
January 2017
INTRODUCTION TO
REDUX
Nacho Martín
@nacmartin
Nacho Martin
I write code at Limenius.
We build tailor-made projects,
and provide consultancy
and formation.
We are very happy with React and React Native.
What is Redux?
@dan_abramov
• State container
• Created by Dan Abramov
• Inspired by Flux and Elm
• Can be used without React
What problem does Redux
solve?
A simple component
A simple component
A simple component
A simple component
A simple component
We have a new requirement
Naive approach 1: pass props
Intermediate components receive props and
pass them to their children without using
them.
Cauliflower
Save
Your name: John
Hi, John
John’s stuff
Cauliflower
Save
Your name: John
Hi, John
John’s stuff
Cauliflower
Save
Your name: John
Hi, John
John’s stuff
state.name
Callback to change name
Naive approach 1: pass props
Intermediate components receive props and
pass them to their children without using
them.
Naive approach 1: pass props
Intermediate components receive props and
pass them to their children without using
them.
Problem: Messy
Difficult to understand the flow of data.
Naive approach 2: use context
Context is a way to pass data down to the
tree, without doing it manually.
Naive approach 2: use context
Context is a way to pass data down to the
tree, without doing it manually.
Problem: Dangerous
Context API is experimental
?
But Redux uses the Context
API internally. Isn’t it a
problem?
?
But Redux uses the Context
API internally. Isn’t it a
problem?
It is better to use libs that
use it than use it directly.
The Redux way
Example
export default class Counter extends Component {
constructor(props) {
super(props);
this.state = {counter: 0};
}
increment() {
this.setState({counter: this.state.counter + 1});
}
decrement() {
this.setState({counter: this.state.counter - 1});
}
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
{this.state.counter}
</Text>
<Button
onPress={this.increment.bind(this)}
title="Increment"
/>
<Button
onPress={this.decrement.bind(this)}
title="Decrement"
/>
</View>
);
}
}
increment() {
this.setState({counter: this.state.counter + 1});
}
decrement() {
this.setState({counter: this.state.counter - 1});
}
What about this?
dispatch(action) {
this.setState(reducer(this.state, action));
}
increment() {
this.dispatch({type: 'INCREMENT'});
}
decrement() {
this.dispatch({type: 'DECREMENT'});
}
What about this?
dispatch(action) {
this.setState(reducer(this.state, action));
}
increment() {
this.dispatch({type: 'INCREMENT'});
}
decrement() {
this.dispatch({type: 'DECREMENT'});
}
Action
What about this?
dispatch(action) {
this.setState(reducer(this.state, action));
}
increment() {
this.dispatch({type: 'INCREMENT'});
}
decrement() {
this.dispatch({type: 'DECREMENT'});
}
const reducer = (state = { counter: 0 }, action) => {
switch (action.type) {
case 'INCREMENT':
return { counter: state.counter + 1 };
case 'DECREMENT':
return { counter: state.counter - 1 };
default:
return state;
}
}
Action
Redux in a picture
Redux in a picture
Install redux
npm install --save redux react-redux
Actions
const increment = {
type: 'INCREMENT'
}
Only source of information from views to change state
Plain JS objects
Must have a type
Actions
Only source of information from views to change state
Plain JS objects
Must have a type
const todoAdd = {
type: 'TODO_ADD',
text: 'Buy some milk',
deadline: '15-01-2017 19:00h'
}
Objects can be as complex as needed, but keep it simple
Reducers
export default reducer = (state = { counter: 0 }, action) => {
switch (action.type) {
case 'INCREMENT':
return {
...state,
counter: state.counter + 1
};
case 'DECREMENT':
return {
...state,
counter: state.counter - 1
};
case 'DUPLICATE':
return {
...state,
counter: state.counter * 2
};
default:
return state;
}
}
From previous state and action produce next state
Store
import { Provider } from 'react-redux'
import { createStore } from 'redux'
import counterReducer from './redux/reducer';
let store = createStore(counterReducer)
export default class CounterApp extends Component {
render() {
return (
<Provider store={store}>
<Counter/>
<Multiplier/>
</Provider>
);
}
}
Connect & dispatch
import { connect } from 'react-redux'
const Counter = (props) => {
return (
<View style={styles.counter}>
<Text>
{props.counter}
</Text>
<Button
onPress={() => {props.dispatch({type: 'INCREMENT'})}}
title="Increment"
/>
<Button
onPress={() => {props.dispatch({type: 'DECREMENT'})}}
title="Decrement"
/>
</View>
);
}
const mapStateToProps = (state) => {
return {
counter: state.counter,
}
};
export default = connect(mapStateToProps)(Counter);
import { connect } from 'react-redux'
const Counter = (props) => {
return (
<View style={styles.counter}>
<Text>
{props.counter}
</Text>
<Button
onPress={() => {props.dispatch({type: 'INCREMENT'})}}
title="Increment"
/>
<Button
onPress={() => {props.dispatch({type: 'DECREMENT'})}}
title="Decrement"
/>
</View>
);
}
const mapStateToProps = (state) => {
return {
counter: state.counter,
}
};
export default connect(mapStateToProps)(Counter);
Connect & dispatch
import { connect } from 'react-redux'
const Counter = (props) => {
return (
<View style={styles.counter}>
<Text>
{props.counter}
</Text>
<Button
onPress={() => {props.dispatch({type: 'INCREMENT'})}}
title="Increment"
/>
<Button
onPress={() => {props.dispatch({type: 'DECREMENT'})}}
title="Decrement"
/>
</View>
);
}
const mapStateToProps = (state) => {
return {
counter: state.counter,
}
};
export default = connect(mapStateToProps)(Counter);
import { connect } from 'react-redux'
const Counter = (props) => {
return (
<View style={styles.counter}>
<Text>
{props.counter}
</Text>
<Button
onPress={() => {props.dispatch({type: 'INCREMENT'})}}
title="Increment"
/>
<Button
onPress={() => {props.dispatch({type: 'DECREMENT'})}}
title="Decrement"
/>
</View>
);
}
const mapStateToProps = (state) => {
return {
counter: state.counter,
}
};
export default connect(mapStateToProps)(Counter);
Export the connected component
Connect & dispatch
import { connect } from 'react-redux'
const Counter = (props) => {
return (
<View style={styles.counter}>
<Text>
{props.counter}
</Text>
<Button
onPress={() => {props.dispatch({type: 'INCREMENT'})}}
title="Increment"
/>
<Button
onPress={() => {props.dispatch({type: 'DECREMENT'})}}
title="Decrement"
/>
</View>
);
}
const mapStateToProps = (state) => {
return {
counter: state.counter,
}
};
export default = connect(mapStateToProps)(Counter);
import { connect } from 'react-redux'
const Counter = (props) => {
return (
<View style={styles.counter}>
<Text>
{props.counter}
</Text>
<Button
onPress={() => {props.dispatch({type: 'INCREMENT'})}}
title="Increment"
/>
<Button
onPress={() => {props.dispatch({type: 'DECREMENT'})}}
title="Decrement"
/>
</View>
);
}
const mapStateToProps = (state) => {
return {
counter: state.counter,
}
};
export default connect(mapStateToProps)(Counter);
Export the connected component
Counter available via props
Connect & dispatch
import { connect } from 'react-redux'
const Counter = (props) => {
return (
<View style={styles.counter}>
<Text>
{props.counter}
</Text>
<Button
onPress={() => {props.dispatch({type: 'INCREMENT'})}}
title="Increment"
/>
<Button
onPress={() => {props.dispatch({type: 'DECREMENT'})}}
title="Decrement"
/>
</View>
);
}
const mapStateToProps = (state) => {
return {
counter: state.counter,
}
};
export default = connect(mapStateToProps)(Counter);
import { connect } from 'react-redux'
const Counter = (props) => {
return (
<View style={styles.counter}>
<Text>
{props.counter}
</Text>
<Button
onPress={() => {props.dispatch({type: 'INCREMENT'})}}
title="Increment"
/>
<Button
onPress={() => {props.dispatch({type: 'DECREMENT'})}}
title="Decrement"
/>
</View>
);
}
const mapStateToProps = (state) => {
return {
counter: state.counter,
}
};
export default connect(mapStateToProps)(Counter);
Export the connected component
Counter available via props
Review
Review
Demo
https://github.com/nacmartin/ReduxIntro
Tips
Keep your reducers pure
• Absolutely deterministic: the same input
produces the same result every time).
• No side effects: no calls to an API, no
changes to the outside world.
Combine reducers
import { combineReducers } from 'redux'
import counterReducer from './counter';
import todoReducer from './todo';
export default combineReducers({
todo,
counter
});
Model your state as a tree and write reducers for its branches
Selectors
const getVisibleTodos = (todos, filter) => {
switch (filter) {
case 'SHOW_ALL':
return todos
case 'SHOW_COMPLETED':
return todos.filter(t => t.completed)
case 'SHOW_ACTIVE':
return todos.filter(t => !t.completed)
}
}
const mapStateToProps = (state) => {
return { todos: getVisibleTodos(state.todos, state.visibilityFilter) }
}
export default VisibleTodoList = connect(
mapStateToProps,
)(TodoList)
Normalize data in the store and use selectors to derive data
Selectors with reselect
import { createSelector } from 'reselect'
const getVisibilityFilter = (state) => state.visibilityFilter
const getTodos = (state) => state.todos
export const getVisibleTodos = createSelector(
[ getVisibilityFilter, getTodos ],
(visibilityFilter, todos) => {
switch (visibilityFilter) {
case 'SHOW_ALL':
return todos
case 'SHOW_COMPLETED':
return todos.filter(t => t.completed)
case 'SHOW_ACTIVE':
return todos.filter(t => !t.completed)
}
}
)
Memoized version, only computed when state.todos or
state.visibilityFilter change
Use constants for action types
Helps us to keep track of existing types, and detect typos
const INCREMENT_COUNTER = 'INCREMENT_COUNTER';
const increment = {
type: INCREMENT_COUNTER
}
import { INCREMENT_COUNTER, DECREMENT } from './actionTypes'
Use action creators
export function increment() {
return {
type: INCREMENT
}
}
export function addTodo(text) {
return {
type: ADD_TODO,
text
}
}
Use action creators
import { connect } from 'react-redux'
const Counter = (props) => {
return (
<View style={styles.counter}>
<Text>
{props.counter}
</Text>
<Button
onPress={() => {props.dispatch({type: 'INCREMENT'})}}
title="Increment"
/>
<Button
onPress={() => {props.dispatch({type: 'DECREMENT'})}}
title="Decrement"
/>
</View>
);
}
const mapStateToProps = (state) => {
return {
counter: state.counter,
}
};
export default = connect(mapStateToProps)(Counter);
import { increment } from ‘../actions';
const Counter = (props) => {
return (
<View style={styles.counter}>
<Button
onPress={() => {props.onIncrementClick()}}
title="Increment" />
</View>
);
}
const mapDispatchToProps = (dispatch) => {
return {
onIncrementClick: () => {
dispatch(increment())
}
}
}
export default connect(mapStateToProps, mapDispatchToProps)(Counter);
ducks-modular-redux
const LOAD = 'my-app/widgets/LOAD';
const CREATE = 'my-app/widgets/CREATE';
const UPDATE = 'my-app/widgets/UPDATE';
const REMOVE = 'my-app/widgets/REMOVE';
// Reducer
export default function reducer(state = {}, action = {}) {
switch (action.type) {
// do reducer stuff
default: return state;
}
}
// Action Creators
export function loadWidgets() {
return { type: LOAD };
}
export function createWidget(widget) {
return { type: CREATE, widget };
}
export function updateWidget(widget) {
return { type: UPDATE, widget };
}
export function removeWidget(widget) {
return { type: REMOVE, widget };
}
ducks-modular-redux
const LOAD = 'my-app/widgets/LOAD';
const CREATE = 'my-app/widgets/CREATE';
const UPDATE = 'my-app/widgets/UPDATE';
const REMOVE = 'my-app/widgets/REMOVE';
// Reducer
export default function reducer(state = {}, action = {}) {
switch (action.type) {
// do reducer stuff
default: return state;
}
}
// Action Creators
export function loadWidgets() {
return { type: LOAD };
}
export function createWidget(widget) {
return { type: CREATE, widget };
}
export function updateWidget(widget) {
return { type: UPDATE, widget };
}
export function removeWidget(widget) {
return { type: REMOVE, widget };
}
Action types
ducks-modular-redux
const LOAD = 'my-app/widgets/LOAD';
const CREATE = 'my-app/widgets/CREATE';
const UPDATE = 'my-app/widgets/UPDATE';
const REMOVE = 'my-app/widgets/REMOVE';
// Reducer
export default function reducer(state = {}, action = {}) {
switch (action.type) {
// do reducer stuff
default: return state;
}
}
// Action Creators
export function loadWidgets() {
return { type: LOAD };
}
export function createWidget(widget) {
return { type: CREATE, widget };
}
export function updateWidget(widget) {
return { type: UPDATE, widget };
}
export function removeWidget(widget) {
return { type: REMOVE, widget };
}
Action types
Reducer
ducks-modular-redux
const LOAD = 'my-app/widgets/LOAD';
const CREATE = 'my-app/widgets/CREATE';
const UPDATE = 'my-app/widgets/UPDATE';
const REMOVE = 'my-app/widgets/REMOVE';
// Reducer
export default function reducer(state = {}, action = {}) {
switch (action.type) {
// do reducer stuff
default: return state;
}
}
// Action Creators
export function loadWidgets() {
return { type: LOAD };
}
export function createWidget(widget) {
return { type: CREATE, widget };
}
export function updateWidget(widget) {
return { type: UPDATE, widget };
}
export function removeWidget(widget) {
return { type: REMOVE, widget };
}
Action types
Reducer
Action creators
ducks-modular-redux
Informed consent
Awesome DevTools
npm install --save-dev remote-redux-devtools
import devToolsEnhancer from 'remote-redux-devtools';
let store = createStore(counterReducer, devToolsEnhancer());
Awesome DevTools
import devToolsEnhancer from 'remote-redux-devtools';
let store = createStore(counterReducer, devToolsEnhancer());
Thanks! @nacmartin
nacho@limenius.com
http://limenius.com

More Related Content

What's hot

React-JS Component Life-cycle Methods
React-JS Component Life-cycle MethodsReact-JS Component Life-cycle Methods
React-JS Component Life-cycle MethodsANKUSH CHAVAN
 
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
 
State management in react applications (Statecharts)
State management in react applications (Statecharts)State management in react applications (Statecharts)
State management in react applications (Statecharts)Tomáš Drenčák
 
React js use contexts and useContext hook
React js use contexts and useContext hookReact js use contexts and useContext hook
React js use contexts and useContext hookPiyush Jamwal
 
Basics of React Hooks.pptx.pdf
Basics of React Hooks.pptx.pdfBasics of React Hooks.pptx.pdf
Basics of React Hooks.pptx.pdfKnoldus Inc.
 
Important React Hooks
Important React HooksImportant React Hooks
Important React HooksKnoldus Inc.
 
Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooksSamundra khatri
 
Getting Started with NgRx (Redux) Angular
Getting Started with NgRx (Redux) AngularGetting Started with NgRx (Redux) Angular
Getting Started with NgRx (Redux) AngularGustavo Costa
 
[Final] ReactJS presentation
[Final] ReactJS presentation[Final] ReactJS presentation
[Final] ReactJS presentation洪 鹏发
 
ReactJS presentation
ReactJS presentationReactJS presentation
ReactJS presentationThanh Tuong
 

What's hot (20)

React workshop
React workshopReact workshop
React workshop
 
Reactjs
Reactjs Reactjs
Reactjs
 
React-JS Component Life-cycle Methods
React-JS Component Life-cycle MethodsReact-JS Component Life-cycle Methods
React-JS Component Life-cycle Methods
 
Introduction to React JS for beginners
Introduction to React JS for beginners Introduction to React JS for beginners
Introduction to React JS for beginners
 
State management in react applications (Statecharts)
State management in react applications (Statecharts)State management in react applications (Statecharts)
State management in react applications (Statecharts)
 
React hooks
React hooksReact hooks
React hooks
 
Its time to React.js
Its time to React.jsIts time to React.js
Its time to React.js
 
React js use contexts and useContext hook
React js use contexts and useContext hookReact js use contexts and useContext hook
React js use contexts and useContext hook
 
ReactJS
ReactJSReactJS
ReactJS
 
Ngrx slides
Ngrx slidesNgrx slides
Ngrx slides
 
Basics of React Hooks.pptx.pdf
Basics of React Hooks.pptx.pdfBasics of React Hooks.pptx.pdf
Basics of React Hooks.pptx.pdf
 
Important React Hooks
Important React HooksImportant React Hooks
Important React Hooks
 
Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooks
 
Getting Started with NgRx (Redux) Angular
Getting Started with NgRx (Redux) AngularGetting Started with NgRx (Redux) Angular
Getting Started with NgRx (Redux) Angular
 
[Final] ReactJS presentation
[Final] ReactJS presentation[Final] ReactJS presentation
[Final] ReactJS presentation
 
Workshop 21: React Router
Workshop 21: React RouterWorkshop 21: React Router
Workshop 21: React Router
 
React JS
React JSReact JS
React JS
 
React hooks
React hooksReact hooks
React hooks
 
ReactJS presentation
ReactJS presentationReactJS presentation
ReactJS presentation
 
React with Redux
React with ReduxReact with Redux
React with Redux
 

Similar to Introduction to Redux

Integrating React.js with PHP projects
Integrating React.js with PHP projectsIntegrating React.js with PHP projects
Integrating React.js with PHP projectsIgnacio Martín
 
Recompacting your react application
Recompacting your react applicationRecompacting your react application
Recompacting your react applicationGreg Bergé
 
React + Redux. Best practices
React + Redux.  Best practicesReact + Redux.  Best practices
React + Redux. Best practicesClickky
 
Battle of React State Managers in frontend applications
Battle of React State Managers in frontend applicationsBattle of React State Managers in frontend applications
Battle of React State Managers in frontend applicationsEvangelia Mitsopoulou
 
Strategies for Mitigating Complexity in React Based Redux Applicaitons
Strategies for Mitigating Complexity in React Based Redux ApplicaitonsStrategies for Mitigating Complexity in React Based Redux Applicaitons
Strategies for Mitigating Complexity in React Based Redux Applicaitonsgarbles
 
N Things You Don't Want to Repeat in React Native
N Things You Don't Want to Repeat in React NativeN Things You Don't Want to Repeat in React Native
N Things You Don't Want to Repeat in React NativeAnton Kulyk
 
Gerenciamento de estado no Angular com NgRx
Gerenciamento de estado no Angular com NgRxGerenciamento de estado no Angular com NgRx
Gerenciamento de estado no Angular com NgRxLoiane Groner
 
Reactive.architecture.with.Angular
Reactive.architecture.with.AngularReactive.architecture.with.Angular
Reactive.architecture.with.AngularEvan Schultz
 
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
 
React new features and intro to Hooks
React new features and intro to HooksReact new features and intro to Hooks
React new features and intro to HooksSoluto
 
Evan Schultz - Angular Camp - ng2-redux
Evan Schultz - Angular Camp - ng2-reduxEvan Schultz - Angular Camp - ng2-redux
Evan Schultz - Angular Camp - ng2-reduxEvan Schultz
 
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 SymfonyIgnacio Martín
 
Matteo Antony Mistretta - React, the Inglorious way - Codemotion Amsterdam 2019
Matteo Antony Mistretta - React, the Inglorious way - Codemotion Amsterdam 2019Matteo Antony Mistretta - React, the Inglorious way - Codemotion Amsterdam 2019
Matteo Antony Mistretta - React, the Inglorious way - Codemotion Amsterdam 2019Codemotion
 
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
 
Indeed My Jobs: A case study in ReactJS and Redux (Meetup talk March 2016)
Indeed My Jobs: A case study in ReactJS and Redux (Meetup talk March 2016)Indeed My Jobs: A case study in ReactJS and Redux (Meetup talk March 2016)
Indeed My Jobs: A case study in ReactJS and Redux (Meetup talk March 2016)indeedeng
 
This is a C# project . I am expected to create as this image shows. .pdf
This is a C# project . I am expected to create as this image shows. .pdfThis is a C# project . I am expected to create as this image shows. .pdf
This is a C# project . I am expected to create as this image shows. .pdfindiaartz
 

Similar to Introduction to Redux (20)

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
 
React + Redux. Best practices
React + Redux.  Best practicesReact + Redux.  Best practices
React + Redux. Best practices
 
Battle of React State Managers in frontend applications
Battle of React State Managers in frontend applicationsBattle of React State Managers in frontend applications
Battle of React State Managers in frontend applications
 
Strategies for Mitigating Complexity in React Based Redux Applicaitons
Strategies for Mitigating Complexity in React Based Redux ApplicaitonsStrategies for Mitigating Complexity in React Based Redux Applicaitons
Strategies for Mitigating Complexity in React Based Redux Applicaitons
 
N Things You Don't Want to Repeat in React Native
N Things You Don't Want to Repeat in React NativeN Things You Don't Want to Repeat in React Native
N Things You Don't Want to Repeat in React Native
 
React redux
React reduxReact redux
React redux
 
Gerenciamento de estado no Angular com NgRx
Gerenciamento de estado no Angular com NgRxGerenciamento de estado no Angular com NgRx
Gerenciamento de estado no Angular com NgRx
 
Reactive.architecture.with.Angular
Reactive.architecture.with.AngularReactive.architecture.with.Angular
Reactive.architecture.with.Angular
 
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 new features and intro to Hooks
React new features and intro to HooksReact new features and intro to Hooks
React new features and intro to Hooks
 
Evan Schultz - Angular Camp - ng2-redux
Evan Schultz - Angular Camp - ng2-reduxEvan Schultz - Angular Camp - ng2-redux
Evan Schultz - Angular Camp - ng2-redux
 
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
 
Advanced redux
Advanced reduxAdvanced redux
Advanced redux
 
ReactJS
ReactJSReactJS
ReactJS
 
Matteo Antony Mistretta - React, the Inglorious way - Codemotion Amsterdam 2019
Matteo Antony Mistretta - React, the Inglorious way - Codemotion Amsterdam 2019Matteo Antony Mistretta - React, the Inglorious way - Codemotion Amsterdam 2019
Matteo Antony Mistretta - React, the Inglorious way - Codemotion Amsterdam 2019
 
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!
 
Indeed My Jobs: A case study in ReactJS and Redux (Meetup talk March 2016)
Indeed My Jobs: A case study in ReactJS and Redux (Meetup talk March 2016)Indeed My Jobs: A case study in ReactJS and Redux (Meetup talk March 2016)
Indeed My Jobs: A case study in ReactJS and Redux (Meetup talk March 2016)
 
This is a C# project . I am expected to create as this image shows. .pdf
This is a C# project . I am expected to create as this image shows. .pdfThis is a C# project . I am expected to create as this image shows. .pdf
This is a C# project . I am expected to create as this image shows. .pdf
 
React 101
React 101React 101
React 101
 

More from Ignacio Martín

Elixir/OTP for PHP developers
Elixir/OTP for PHP developersElixir/OTP for PHP developers
Elixir/OTP for PHP developersIgnacio Martín
 
Introduction to React Native Workshop
Introduction to React Native WorkshopIntroduction to React Native Workshop
Introduction to React Native WorkshopIgnacio Martín
 
Symfony 4 Workshop - Limenius
Symfony 4 Workshop - LimeniusSymfony 4 Workshop - Limenius
Symfony 4 Workshop - LimeniusIgnacio Martín
 
Server Side Rendering of JavaScript in PHP
Server Side Rendering of JavaScript in PHPServer Side Rendering of JavaScript in PHP
Server Side Rendering of JavaScript in PHPIgnacio Martín
 
Extending Redux in the Server Side
Extending Redux in the Server SideExtending Redux in the Server Side
Extending Redux in the Server SideIgnacio Martín
 
Redux Sagas - React Alicante
Redux Sagas - React AlicanteRedux Sagas - React Alicante
Redux Sagas - React AlicanteIgnacio Martín
 
React Native Workshop - React Alicante
React Native Workshop - React AlicanteReact Native Workshop - React Alicante
React Native Workshop - React AlicanteIgnacio Martín
 
Asegurando APIs en Symfony con JWT
Asegurando APIs en Symfony con JWTAsegurando APIs en Symfony con JWT
Asegurando APIs en Symfony con JWTIgnacio Martín
 
Redux saga: managing your side effects. Also: generators in es6
Redux saga: managing your side effects. Also: generators in es6Redux saga: managing your side effects. Also: generators in es6
Redux saga: managing your side effects. Also: generators in es6Ignacio Martín
 
Keeping the frontend under control with Symfony and Webpack
Keeping the frontend under control with Symfony and WebpackKeeping the frontend under control with Symfony and Webpack
Keeping the frontend under control with Symfony and WebpackIgnacio Martín
 
Integrando React.js en aplicaciones Symfony (deSymfony 2016)
Integrando React.js en aplicaciones Symfony (deSymfony 2016)Integrando React.js en aplicaciones Symfony (deSymfony 2016)
Integrando React.js en aplicaciones Symfony (deSymfony 2016)Ignacio Martín
 
Adding Realtime to your Projects
Adding Realtime to your ProjectsAdding Realtime to your Projects
Adding Realtime to your ProjectsIgnacio Martín
 
Symfony & Javascript. Combining the best of two worlds
Symfony & Javascript. Combining the best of two worldsSymfony & Javascript. Combining the best of two worlds
Symfony & Javascript. Combining the best of two worldsIgnacio Martín
 

More from Ignacio Martín (16)

Elixir/OTP for PHP developers
Elixir/OTP for PHP developersElixir/OTP for PHP developers
Elixir/OTP for PHP developers
 
Introduction to React Native Workshop
Introduction to React Native WorkshopIntroduction to React Native Workshop
Introduction to React Native Workshop
 
Symfony 4 Workshop - Limenius
Symfony 4 Workshop - LimeniusSymfony 4 Workshop - Limenius
Symfony 4 Workshop - Limenius
 
Server Side Rendering of JavaScript in PHP
Server Side Rendering of JavaScript in PHPServer Side Rendering of JavaScript in PHP
Server Side Rendering of JavaScript in PHP
 
Extending Redux in the Server Side
Extending Redux in the Server SideExtending Redux in the Server Side
Extending Redux in the Server Side
 
Redux Sagas - React Alicante
Redux Sagas - React AlicanteRedux Sagas - React Alicante
Redux Sagas - React Alicante
 
React Native Workshop - React Alicante
React Native Workshop - React AlicanteReact Native Workshop - React Alicante
React Native Workshop - React Alicante
 
Asegurando APIs en Symfony con JWT
Asegurando APIs en Symfony con JWTAsegurando APIs en Symfony con JWT
Asegurando APIs en Symfony con JWT
 
Redux saga: managing your side effects. Also: generators in es6
Redux saga: managing your side effects. Also: generators in es6Redux saga: managing your side effects. Also: generators in es6
Redux saga: managing your side effects. Also: generators in es6
 
Keeping the frontend under control with Symfony and Webpack
Keeping the frontend under control with Symfony and WebpackKeeping the frontend under control with Symfony and Webpack
Keeping the frontend under control with Symfony and Webpack
 
Integrando React.js en aplicaciones Symfony (deSymfony 2016)
Integrando React.js en aplicaciones Symfony (deSymfony 2016)Integrando React.js en aplicaciones Symfony (deSymfony 2016)
Integrando React.js en aplicaciones Symfony (deSymfony 2016)
 
Adding Realtime to your Projects
Adding Realtime to your ProjectsAdding Realtime to your Projects
Adding Realtime to your Projects
 
Symfony & Javascript. Combining the best of two worlds
Symfony & Javascript. Combining the best of two worldsSymfony & Javascript. Combining the best of two worlds
Symfony & Javascript. Combining the best of two worlds
 
Symfony 2 CMF
Symfony 2 CMFSymfony 2 CMF
Symfony 2 CMF
 
Doctrine2 sf2Vigo
Doctrine2 sf2VigoDoctrine2 sf2Vigo
Doctrine2 sf2Vigo
 
Presentacion git
Presentacion gitPresentacion git
Presentacion git
 

Recently uploaded

Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 

Recently uploaded (20)

Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 

Introduction to Redux

  • 1. React Native Munich Meetup January 2017 INTRODUCTION TO REDUX Nacho Martín @nacmartin
  • 2. Nacho Martin I write code at Limenius. We build tailor-made projects, and provide consultancy and formation. We are very happy with React and React Native.
  • 4. @dan_abramov • State container • Created by Dan Abramov • Inspired by Flux and Elm • Can be used without React
  • 5. What problem does Redux solve?
  • 11. We have a new requirement
  • 12. Naive approach 1: pass props Intermediate components receive props and pass them to their children without using them.
  • 15. Cauliflower Save Your name: John Hi, John John’s stuff state.name Callback to change name
  • 16. Naive approach 1: pass props Intermediate components receive props and pass them to their children without using them.
  • 17. Naive approach 1: pass props Intermediate components receive props and pass them to their children without using them. Problem: Messy Difficult to understand the flow of data.
  • 18. Naive approach 2: use context Context is a way to pass data down to the tree, without doing it manually.
  • 19. Naive approach 2: use context Context is a way to pass data down to the tree, without doing it manually. Problem: Dangerous Context API is experimental
  • 20. ? But Redux uses the Context API internally. Isn’t it a problem?
  • 21. ? But Redux uses the Context API internally. Isn’t it a problem? It is better to use libs that use it than use it directly.
  • 23. Example export default class Counter extends Component { constructor(props) { super(props); this.state = {counter: 0}; } increment() { this.setState({counter: this.state.counter + 1}); } decrement() { this.setState({counter: this.state.counter - 1}); } render() { return ( <View style={styles.container}> <Text style={styles.welcome}> {this.state.counter} </Text> <Button onPress={this.increment.bind(this)} title="Increment" /> <Button onPress={this.decrement.bind(this)} title="Decrement" /> </View> ); } }
  • 24. increment() { this.setState({counter: this.state.counter + 1}); } decrement() { this.setState({counter: this.state.counter - 1}); }
  • 25. What about this? dispatch(action) { this.setState(reducer(this.state, action)); } increment() { this.dispatch({type: 'INCREMENT'}); } decrement() { this.dispatch({type: 'DECREMENT'}); }
  • 26. What about this? dispatch(action) { this.setState(reducer(this.state, action)); } increment() { this.dispatch({type: 'INCREMENT'}); } decrement() { this.dispatch({type: 'DECREMENT'}); } Action
  • 27. What about this? dispatch(action) { this.setState(reducer(this.state, action)); } increment() { this.dispatch({type: 'INCREMENT'}); } decrement() { this.dispatch({type: 'DECREMENT'}); } const reducer = (state = { counter: 0 }, action) => { switch (action.type) { case 'INCREMENT': return { counter: state.counter + 1 }; case 'DECREMENT': return { counter: state.counter - 1 }; default: return state; } } Action
  • 28. Redux in a picture
  • 29. Redux in a picture
  • 30. Install redux npm install --save redux react-redux
  • 31. Actions const increment = { type: 'INCREMENT' } Only source of information from views to change state Plain JS objects Must have a type
  • 32. Actions Only source of information from views to change state Plain JS objects Must have a type const todoAdd = { type: 'TODO_ADD', text: 'Buy some milk', deadline: '15-01-2017 19:00h' } Objects can be as complex as needed, but keep it simple
  • 33. Reducers export default reducer = (state = { counter: 0 }, action) => { switch (action.type) { case 'INCREMENT': return { ...state, counter: state.counter + 1 }; case 'DECREMENT': return { ...state, counter: state.counter - 1 }; case 'DUPLICATE': return { ...state, counter: state.counter * 2 }; default: return state; } } From previous state and action produce next state
  • 34. Store import { Provider } from 'react-redux' import { createStore } from 'redux' import counterReducer from './redux/reducer'; let store = createStore(counterReducer) export default class CounterApp extends Component { render() { return ( <Provider store={store}> <Counter/> <Multiplier/> </Provider> ); } }
  • 35. Connect & dispatch import { connect } from 'react-redux' const Counter = (props) => { return ( <View style={styles.counter}> <Text> {props.counter} </Text> <Button onPress={() => {props.dispatch({type: 'INCREMENT'})}} title="Increment" /> <Button onPress={() => {props.dispatch({type: 'DECREMENT'})}} title="Decrement" /> </View> ); } const mapStateToProps = (state) => { return { counter: state.counter, } }; export default = connect(mapStateToProps)(Counter); import { connect } from 'react-redux' const Counter = (props) => { return ( <View style={styles.counter}> <Text> {props.counter} </Text> <Button onPress={() => {props.dispatch({type: 'INCREMENT'})}} title="Increment" /> <Button onPress={() => {props.dispatch({type: 'DECREMENT'})}} title="Decrement" /> </View> ); } const mapStateToProps = (state) => { return { counter: state.counter, } }; export default connect(mapStateToProps)(Counter);
  • 36. Connect & dispatch import { connect } from 'react-redux' const Counter = (props) => { return ( <View style={styles.counter}> <Text> {props.counter} </Text> <Button onPress={() => {props.dispatch({type: 'INCREMENT'})}} title="Increment" /> <Button onPress={() => {props.dispatch({type: 'DECREMENT'})}} title="Decrement" /> </View> ); } const mapStateToProps = (state) => { return { counter: state.counter, } }; export default = connect(mapStateToProps)(Counter); import { connect } from 'react-redux' const Counter = (props) => { return ( <View style={styles.counter}> <Text> {props.counter} </Text> <Button onPress={() => {props.dispatch({type: 'INCREMENT'})}} title="Increment" /> <Button onPress={() => {props.dispatch({type: 'DECREMENT'})}} title="Decrement" /> </View> ); } const mapStateToProps = (state) => { return { counter: state.counter, } }; export default connect(mapStateToProps)(Counter); Export the connected component
  • 37. Connect & dispatch import { connect } from 'react-redux' const Counter = (props) => { return ( <View style={styles.counter}> <Text> {props.counter} </Text> <Button onPress={() => {props.dispatch({type: 'INCREMENT'})}} title="Increment" /> <Button onPress={() => {props.dispatch({type: 'DECREMENT'})}} title="Decrement" /> </View> ); } const mapStateToProps = (state) => { return { counter: state.counter, } }; export default = connect(mapStateToProps)(Counter); import { connect } from 'react-redux' const Counter = (props) => { return ( <View style={styles.counter}> <Text> {props.counter} </Text> <Button onPress={() => {props.dispatch({type: 'INCREMENT'})}} title="Increment" /> <Button onPress={() => {props.dispatch({type: 'DECREMENT'})}} title="Decrement" /> </View> ); } const mapStateToProps = (state) => { return { counter: state.counter, } }; export default connect(mapStateToProps)(Counter); Export the connected component Counter available via props
  • 38. Connect & dispatch import { connect } from 'react-redux' const Counter = (props) => { return ( <View style={styles.counter}> <Text> {props.counter} </Text> <Button onPress={() => {props.dispatch({type: 'INCREMENT'})}} title="Increment" /> <Button onPress={() => {props.dispatch({type: 'DECREMENT'})}} title="Decrement" /> </View> ); } const mapStateToProps = (state) => { return { counter: state.counter, } }; export default = connect(mapStateToProps)(Counter); import { connect } from 'react-redux' const Counter = (props) => { return ( <View style={styles.counter}> <Text> {props.counter} </Text> <Button onPress={() => {props.dispatch({type: 'INCREMENT'})}} title="Increment" /> <Button onPress={() => {props.dispatch({type: 'DECREMENT'})}} title="Decrement" /> </View> ); } const mapStateToProps = (state) => { return { counter: state.counter, } }; export default connect(mapStateToProps)(Counter); Export the connected component Counter available via props
  • 42. Tips
  • 43. Keep your reducers pure • Absolutely deterministic: the same input produces the same result every time). • No side effects: no calls to an API, no changes to the outside world.
  • 44. Combine reducers import { combineReducers } from 'redux' import counterReducer from './counter'; import todoReducer from './todo'; export default combineReducers({ todo, counter }); Model your state as a tree and write reducers for its branches
  • 45. Selectors const getVisibleTodos = (todos, filter) => { switch (filter) { case 'SHOW_ALL': return todos case 'SHOW_COMPLETED': return todos.filter(t => t.completed) case 'SHOW_ACTIVE': return todos.filter(t => !t.completed) } } const mapStateToProps = (state) => { return { todos: getVisibleTodos(state.todos, state.visibilityFilter) } } export default VisibleTodoList = connect( mapStateToProps, )(TodoList) Normalize data in the store and use selectors to derive data
  • 46. Selectors with reselect import { createSelector } from 'reselect' const getVisibilityFilter = (state) => state.visibilityFilter const getTodos = (state) => state.todos export const getVisibleTodos = createSelector( [ getVisibilityFilter, getTodos ], (visibilityFilter, todos) => { switch (visibilityFilter) { case 'SHOW_ALL': return todos case 'SHOW_COMPLETED': return todos.filter(t => t.completed) case 'SHOW_ACTIVE': return todos.filter(t => !t.completed) } } ) Memoized version, only computed when state.todos or state.visibilityFilter change
  • 47. Use constants for action types Helps us to keep track of existing types, and detect typos const INCREMENT_COUNTER = 'INCREMENT_COUNTER'; const increment = { type: INCREMENT_COUNTER } import { INCREMENT_COUNTER, DECREMENT } from './actionTypes'
  • 48. Use action creators export function increment() { return { type: INCREMENT } } export function addTodo(text) { return { type: ADD_TODO, text } }
  • 49. Use action creators import { connect } from 'react-redux' const Counter = (props) => { return ( <View style={styles.counter}> <Text> {props.counter} </Text> <Button onPress={() => {props.dispatch({type: 'INCREMENT'})}} title="Increment" /> <Button onPress={() => {props.dispatch({type: 'DECREMENT'})}} title="Decrement" /> </View> ); } const mapStateToProps = (state) => { return { counter: state.counter, } }; export default = connect(mapStateToProps)(Counter); import { increment } from ‘../actions'; const Counter = (props) => { return ( <View style={styles.counter}> <Button onPress={() => {props.onIncrementClick()}} title="Increment" /> </View> ); } const mapDispatchToProps = (dispatch) => { return { onIncrementClick: () => { dispatch(increment()) } } } export default connect(mapStateToProps, mapDispatchToProps)(Counter);
  • 50. ducks-modular-redux const LOAD = 'my-app/widgets/LOAD'; const CREATE = 'my-app/widgets/CREATE'; const UPDATE = 'my-app/widgets/UPDATE'; const REMOVE = 'my-app/widgets/REMOVE'; // Reducer export default function reducer(state = {}, action = {}) { switch (action.type) { // do reducer stuff default: return state; } } // Action Creators export function loadWidgets() { return { type: LOAD }; } export function createWidget(widget) { return { type: CREATE, widget }; } export function updateWidget(widget) { return { type: UPDATE, widget }; } export function removeWidget(widget) { return { type: REMOVE, widget }; }
  • 51. ducks-modular-redux const LOAD = 'my-app/widgets/LOAD'; const CREATE = 'my-app/widgets/CREATE'; const UPDATE = 'my-app/widgets/UPDATE'; const REMOVE = 'my-app/widgets/REMOVE'; // Reducer export default function reducer(state = {}, action = {}) { switch (action.type) { // do reducer stuff default: return state; } } // Action Creators export function loadWidgets() { return { type: LOAD }; } export function createWidget(widget) { return { type: CREATE, widget }; } export function updateWidget(widget) { return { type: UPDATE, widget }; } export function removeWidget(widget) { return { type: REMOVE, widget }; } Action types
  • 52. ducks-modular-redux const LOAD = 'my-app/widgets/LOAD'; const CREATE = 'my-app/widgets/CREATE'; const UPDATE = 'my-app/widgets/UPDATE'; const REMOVE = 'my-app/widgets/REMOVE'; // Reducer export default function reducer(state = {}, action = {}) { switch (action.type) { // do reducer stuff default: return state; } } // Action Creators export function loadWidgets() { return { type: LOAD }; } export function createWidget(widget) { return { type: CREATE, widget }; } export function updateWidget(widget) { return { type: UPDATE, widget }; } export function removeWidget(widget) { return { type: REMOVE, widget }; } Action types Reducer
  • 53. ducks-modular-redux const LOAD = 'my-app/widgets/LOAD'; const CREATE = 'my-app/widgets/CREATE'; const UPDATE = 'my-app/widgets/UPDATE'; const REMOVE = 'my-app/widgets/REMOVE'; // Reducer export default function reducer(state = {}, action = {}) { switch (action.type) { // do reducer stuff default: return state; } } // Action Creators export function loadWidgets() { return { type: LOAD }; } export function createWidget(widget) { return { type: CREATE, widget }; } export function updateWidget(widget) { return { type: UPDATE, widget }; } export function removeWidget(widget) { return { type: REMOVE, widget }; } Action types Reducer Action creators
  • 55. Awesome DevTools npm install --save-dev remote-redux-devtools import devToolsEnhancer from 'remote-redux-devtools'; let store = createStore(counterReducer, devToolsEnhancer());
  • 56. Awesome DevTools import devToolsEnhancer from 'remote-redux-devtools'; let store = createStore(counterReducer, devToolsEnhancer());