Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. const initialState = {
  2. counter:0,
  3. }
  4.  
  5. const store = createStore(reduce);
  6.  
  7. const reduce = (state = initialState, action) => {
  8. switch(action.type) {
  9. case 'ACTION_CHANGE_COUNTER':
  10. return {...state, counter: ++action.payload };
  11. case 'RESET_COUNTER':
  12. return {...state, counter: action.payload};
  13. }
  14. return state;
  15. }
  16.  
  17. const ACTION_CHANGE_COUNTER = 'ACTION_CHANGE_COUNTER';
  18. const RESET_COUNTER = 'RESET_COUNTER';
  19.  
  20. const changeCounter = {
  21. type: ACTION_CHANGE_COUNTER,
  22. payload: initialState.counter
  23. }
  24.  
  25. const resetCounter = {
  26. type: RESET_COUNTER,
  27. payload: 0
  28. }
  29.  
  30.  
  31. class MainComponent extends React.Component {
  32. constructor(props) {
  33. super(props);
  34. this.press = this.press.bind(this);
  35. this.reset = this.reset.bind(this);
  36. }
  37. press() {
  38. store.dispatch(changeCounter)
  39. }
  40. reset() {
  41. store.dispatch(resetCounter) // reset срабатывает и на экране 0, но потом счет начинается не с нуля а с числа где был резет
  42. }
  43. render() {
  44. return <div>
  45. {
  46. this.props.counter
  47. }
  48. <button onClick={this.press}>ok</button>
  49. <button onClick={this.reset}>reset</button>
  50. </div>
  51. }
  52. }
  53.  
  54. function mapStateToProps (state) {
  55. console.log(state)
  56. return {
  57. counter: state.counter,
  58. }
  59. }
  60. const WrapMainComponent = connect(mapStateToProps)(MainComponent);
  61.  
  62.  
  63. ReactDOM.render(<Provider store={store}><WrapMainComponent/></Provider>
  64. , document.getElementById('app')
  65. )
  66.  
  67. const reduce = (state = initialState, action) => {
  68. switch (action.type) {
  69. case "ACTION_CHANGE_COUNTER":
  70. return { ...state, counter: state.counter + 1 };
  71. case "RESET_COUNTER":
  72. return { ...state, counter: 0 };
  73. }
  74. return state;
  75. };
  76.  
  77. const ACTION_CHANGE_COUNTER = "ACTION_CHANGE_COUNTER";
  78. const RESET_COUNTER = "RESET_COUNTER";
  79.  
  80. const changeCounter = {
  81. type: ACTION_CHANGE_COUNTER,
  82. };
  83.  
  84. const resetCounter = {
  85. type: RESET_COUNTER,
  86. };
  87.  
  88. const reduce = (state = initialState, action) => {
  89. switch (action.type) {
  90. case "ACTION_CHANGE_COUNTER":
  91. return { ...state, counter: action.payload };
  92. case "RESET_COUNTER":
  93. return { ...state, counter: 0 };
  94. }
  95. return state;
  96. };
  97.  
  98. ....
  99.  
  100. const changeCounter = (counter) => ({
  101. type: ACTION_CHANGE_COUNTER,
  102. payload: counter,
  103. })
  104. ....
  105.  
  106. press() {
  107. store.dispatch(changeCounter(this.props.counter + 1));
  108. }
  109.  
  110. import { createStore } from "redux";
  111. import React from "react";
  112. import ReactDOM from "react-dom";
  113. import { connect, Provider } from "react-redux";
  114.  
  115. const initialState = {
  116. counter: 0
  117. };
  118.  
  119. const reduce = (state = initialState, action) => {
  120. switch (action.type) {
  121. case "ACTION_CHANGE_COUNTER":
  122. return { ...state, counter: action.payload };
  123. case "RESET_COUNTER":
  124. return { ...state, counter: 0 };
  125. }
  126. return state;
  127. };
  128.  
  129. const ACTION_CHANGE_COUNTER = "ACTION_CHANGE_COUNTER";
  130. const RESET_COUNTER = "RESET_COUNTER";
  131.  
  132. const changeCounter = (counter) => ({
  133. type: ACTION_CHANGE_COUNTER,
  134. payload: counter,
  135. })
  136.  
  137. const resetCounter = () => ({
  138. type: RESET_COUNTER,
  139. })
  140.  
  141. // кроме рендера ничего нет, можно заменить класс на функцию
  142. class MainComponent extends React.Component {
  143. render() {
  144. return (
  145. <div>
  146. {this.props.counter}
  147. <button onClick={() => this.props.changeCounter(this.props.counter + 1)}>ok</button>
  148. <button onClick={this.props.resetCounter}>reset</button>
  149. </div>
  150. );
  151. }
  152. }
  153.  
  154. function mapStateToProps(state) {
  155. console.log(state);
  156. return {
  157. counter: state.counter
  158. };
  159. }
  160.  
  161. const WrapMainComponent = connect(mapStateToProps,
  162. { changeCounter, resetCounter }
  163. )(MainComponent);
  164.  
  165. const store = createStore(reduce);
  166.  
  167. ReactDOM.render(
  168. <Provider store={store}>
  169. <WrapMainComponent />
  170. </Provider>,
  171. document.getElementById("app")
  172. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement