Advertisement
Guest User

Untitled

a guest
May 27th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. /**
  2. * Folder structure
  3. */
  4.  
  5. src
  6. -- redux
  7. -- -- action
  8. -- -- -- builder.js
  9. -- -- -- counter.js
  10. -- -- reducer
  11. -- -- -- builder.js
  12. -- -- -- counter.js
  13. -- -- -- rootReducer.js
  14.  
  15. /**
  16. * 1. Create the reducer: reducer/builder.js
  17. */
  18.  
  19. // Use immer for create the next immutable state tree by simply modifying the current tree.
  20. import produce from 'immer';
  21.  
  22. // The state should always be defined.
  23. const initialState = {
  24. arr: ""
  25. }
  26.  
  27. export default function builder (state = initialState, action) {
  28. console.log('reducer', state, action)
  29. switch(action.type) {
  30. case 'PUSH':
  31. return produce(state, draft => {
  32. draft.arr += 'X'
  33. })
  34. case 'SLICE':
  35. return produce(state, draft => {
  36. draft.arr = draft.arr.substring(0, draft.arr.length-1)
  37. })
  38. case 'CLEAR':
  39. return produce(state, draft => {
  40. draft.arr = ""
  41. })
  42. default:
  43. return state
  44. }
  45. }
  46.  
  47. /**
  48. * 2. Create the action creators and types: action/builder.js
  49. */
  50.  
  51. /**
  52. * Action Types
  53. * Sometimes it's a good idea to put them in a seperate file.
  54. */
  55.  
  56. export const PUSH = 'PUSH'
  57. export const SLICE = 'SLICE'
  58. export const CLEAR = 'CLEAR'
  59.  
  60. /**
  61. * Action Creators
  62. */
  63.  
  64. export function push () {
  65. return {
  66. type: PUSH
  67. }
  68. }
  69.  
  70. export const slice = () => {
  71. return {
  72. type: SLICE
  73. }
  74. }
  75.  
  76. export const clear = () => ({ type: CLEAR })
  77.  
  78. /**
  79. * 3. Register the reducer at the rootReducer: reducer/rootReducer.js
  80. */
  81.  
  82. import { combineReducers } from "redux";
  83. import counter from "./counter"
  84. import builder from "./builder"
  85.  
  86.  
  87. export default combineReducers({
  88. counter,
  89. builder
  90. });
  91.  
  92. /**
  93. * 4. Connect your components: Builder.js
  94. */
  95.  
  96. import React from 'react'
  97. import { connect } from 'react-redux'
  98.  
  99. import { push, slice, clear } from './redux/actions/builder'
  100.  
  101. const mapStateToProps = (state) => {
  102. return {
  103. arr: state.builder.arr,
  104. }
  105. }
  106.  
  107. const mapDispatchToProps = {
  108. push,
  109. slice,
  110. clear
  111. }
  112.  
  113. function Builder (props) {
  114. // const [count, setCount] = useState(0)
  115. // now count is part of the state (this.state.count)
  116. // not a setter is available: setCount(0)
  117. // useState(<initialValue>)
  118.  
  119. const _push = () => {
  120. props.push()
  121. }
  122.  
  123. const _slice = () => {
  124. props.slice()
  125. }
  126.  
  127. const _clear = () => {
  128. props.clear()
  129. }
  130.  
  131. return (
  132. <div>
  133. <h2>Builder</h2>
  134. <div>
  135. <button onClick={_slice}>-</button>
  136. <span>{props.arr}</span>
  137. <button onClick={_push}>+</button>
  138. </div>
  139. <div>
  140. <button onClick={_clear}>Reset</button>
  141. </div>
  142. </div>
  143. )
  144. }
  145.  
  146. export default connect(mapStateToProps, mapDispatchToProps)(Builder)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement