Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react'
  2. import { connect } from 'react-redux'
  3.  
  4.  
  5. // --- Reducer.js
  6. const defaultReduxState = {
  7.   incFactor: 1,
  8.   value: 0,
  9. };
  10.  
  11. const simpleReducer = (state = defaultReduxState , action) => {
  12.  switch (action.type) {
  13.   case 'INCREMENT': return {...state, value: state.value + state.incFactor };
  14.   case 'DECREMENT': return {...state, value: state.value - state.incFactor };
  15.   default: return state;
  16.  }
  17. }
  18. // --- end of Reducer.js
  19.  
  20.  
  21. // --- Action.js
  22. const incrementAction = () => {
  23.   return {
  24.     type: 'INCREMENT'
  25.   };
  26. }
  27.  
  28. const decrementAction = () => {
  29.   return {
  30.     type: 'DECREMENT'
  31.   };
  32. }
  33. // --- end of Action.js
  34.  
  35.  
  36. // --- Component.js
  37. class MyComponent extends React.Component {
  38.   handleClick = (mode) => {
  39.     if (mode === 'inc') {
  40.       this.props.doIncrement();
  41.     } else if (mode === 'dec') {
  42.       this.props.doDecrement();
  43.     }
  44.   }
  45.  
  46.   render() {
  47.     <div>
  48.       {this.props.counter}
  49.       <button onClick={this.handleClick('inc')}>Increment</button>
  50.       <button onClick={this.handleClick('dec')}>Decrement</button>
  51.     </div>
  52.   }
  53. }
  54.  
  55. const mapStateToProps = state => {
  56.   return { counter: state.value };
  57. }
  58.  
  59. const mapDispatchToProps = dispatch => {
  60.   return {
  61.     doIncrement: () => dispatch(incrementAction()),
  62.     doDecrement: () => dispatch(decrementAction())
  63.   };
  64. }
  65.  
  66. export connect(mapStateToProps, mapDispatchToProps)(MyComponent)
  67. // --- end of Component.js
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement