Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Introduction to Redux

3,975 views

Published on

Explanation of the fundamentals of Redux with additional tips and good practices. Presented in the Munich React Native Meetup, so the sample code is using React Native. Additional code: https://github.com/nacmartin/ReduxIntro

Published in: Technology
  • I'd advise you to use this service: ⇒ www.HelpWriting.net ⇐ The price of your order will depend on the deadline and type of paper (e.g. bachelor, undergraduate etc). The more time you have before the deadline - the less price of the order you will have. Thus, this service offers high-quality essays at the optimal price.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • You can try to use this service ⇒ www.HelpWriting.net ⇐ I have used it several times in college and was absolutely satisfied with the result.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD THE BOOK INTO AVAILABLE FORMAT (New Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://urlzs.com/UABbn } ......................................................................................................................... Download Full EPUB Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download Full doc Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download PDF EBOOK here { https://urlzs.com/UABbn } ......................................................................................................................... Download EPUB Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download doc Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book THE can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer THE is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBOOK .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, CookBOOK, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, EBOOK, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story THE Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money THE the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths THE Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Introduction to Redux

  1. 1. React Native Munich Meetup January 2017 INTRODUCTION TO REDUX Nacho Martín @nacmartin
  2. 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.
  3. 3. What is Redux?
  4. 4. @dan_abramov • State container • Created by Dan Abramov • Inspired by Flux and Elm • Can be used without React
  5. 5. What problem does Redux solve?
  6. 6. A simple component
  7. 7. A simple component
  8. 8. A simple component
  9. 9. A simple component
  10. 10. A simple component
  11. 11. We have a new requirement
  12. 12. Naive approach 1: pass props Intermediate components receive props and pass them to their children without using them.
  13. 13. Cauliflower Save Your name: John Hi, John John’s stuff
  14. 14. Cauliflower Save Your name: John Hi, John John’s stuff
  15. 15. Cauliflower Save Your name: John Hi, John John’s stuff state.name Callback to change name
  16. 16. Naive approach 1: pass props Intermediate components receive props and pass them to their children without using them.
  17. 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. 18. Naive approach 2: use context Context is a way to pass data down to the tree, without doing it manually.
  19. 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. 20. ? But Redux uses the Context API internally. Isn’t it a problem?
  21. 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.
  22. 22. The Redux way
  23. 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. 24. increment() { this.setState({counter: this.state.counter + 1}); } decrement() { this.setState({counter: this.state.counter - 1}); }
  25. 25. What about this? dispatch(action) { this.setState(reducer(this.state, action)); } increment() { this.dispatch({type: 'INCREMENT'}); } decrement() { this.dispatch({type: 'DECREMENT'}); }
  26. 26. What about this? dispatch(action) { this.setState(reducer(this.state, action)); } increment() { this.dispatch({type: 'INCREMENT'}); } decrement() { this.dispatch({type: 'DECREMENT'}); } Action
  27. 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. 28. Redux in a picture
  29. 29. Redux in a picture
  30. 30. Install redux npm install --save redux react-redux
  31. 31. Actions const increment = { type: 'INCREMENT' } Only source of information from views to change state Plain JS objects Must have a type
  32. 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. 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. 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. 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. 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. 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. 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
  39. 39. Review
  40. 40. Review
  41. 41. Demo https://github.com/nacmartin/ReduxIntro
  42. 42. Tips
  43. 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. 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. 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. 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. 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. 48. Use action creators export function increment() { return { type: INCREMENT } } export function addTodo(text) { return { type: ADD_TODO, text } }
  49. 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. 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. 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. 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. 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
  54. 54. ducks-modular-redux Informed consent
  55. 55. Awesome DevTools npm install --save-dev remote-redux-devtools import devToolsEnhancer from 'remote-redux-devtools'; let store = createStore(counterReducer, devToolsEnhancer());
  56. 56. Awesome DevTools import devToolsEnhancer from 'remote-redux-devtools'; let store = createStore(counterReducer, devToolsEnhancer());
  57. 57. Thanks! @nacmartin nacho@limenius.com http://limenius.com

×