Guest User

Untitled

a guest
Nov 17th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. Make a dispatcher:
  2.  
  3. ```js
  4. import { Dispatcher } from 'flux';
  5.  
  6. export default new Dispatcher();
  7. ```
  8.  
  9. Set up the actions:
  10.  
  11. ```js
  12. import AppDispatcher from './AppDispatcher';
  13.  
  14. export const updateNumberOfPeople = (value) => {
  15. AppDispatcher.dispatch({
  16. type: 'UPDATE_NUMBER_OF_PEOPLE',
  17. value,
  18. });
  19. }
  20.  
  21. export const updateSlicesPerPerson = (value) => {
  22. AppDispatcher.dispatch({
  23. type: 'UPDATE_SLICES_PER_PERSON',
  24. value,
  25. });
  26. }
  27.  
  28. export const reset = (value) => {
  29. AppDispatcher.dispatch({
  30. type: 'RESET',
  31. });
  32. }
  33. ```
  34.  
  35. Let's start the store:
  36.  
  37. ```js
  38. import EventEmitter from 'events';
  39. import { AppDispatcher } from './AppDispatcher';
  40.  
  41. export default class PizzaCalculatorStore extends EventEmitter {
  42. constructor() {
  43. super();
  44. }
  45. }
  46. ```
  47.  
  48. Implement the actions:
  49.  
  50. ```js
  51. export default class PizzaCalculatorStore extends EventEmitter {
  52. constructor() {
  53. super();
  54.  
  55. AppDispatcher.register((action) => {
  56. if (action.type === 'UPDATE_NUMBER_OF_PEOPLE') {
  57. calculator.numberOfPeople = action.value;
  58. this.emit('change');
  59. }
  60.  
  61. if (action.type === 'UPDATE_SLICES_PER_PERSON') {
  62. calculator.slicesPerPerson = action.value;
  63. this.emit('change');
  64. }
  65.  
  66. if (action.type === 'RESET') {
  67. calculator = { ...initialState };
  68. this.emit('change');
  69. }
  70. });
  71. }
  72. }
  73. ```
  74.  
  75. Export it.
  76.  
  77. ```js
  78. export default new PizzaCalculatorStore();
  79. ```
  80.  
  81. Let's use the container pattern!
  82.  
  83. ```js
  84. class ApplicationContainer extends Component {
  85. state = pizzaCalculator.getState();
  86.  
  87. updateCalculations = () => {
  88. this.setState(pizzaCalculator.getState());
  89. }
  90.  
  91. componentDidMount() {
  92. pizzaCalculator.on('change', this.updateCalculations);
  93. }
  94.  
  95. componentWillUnmount() {
  96. pizzaCalculator.off('change', this.updateCalculations);
  97. }
  98.  
  99. render() {
  100. return (
  101. <PizzaCalculator
  102. {...this.state}
  103. updateNumberOfPeople={actions.updateNumberOfPeople}
  104. updateSlicesPerPerson={actions.updateSlicesPerPerson}
  105. reset={actions.reset}
  106. />
  107. );
  108. }
  109. }
  110. ```
  111.  
  112. Cool, now we can refactor the presenter to receive props. We also want it to handle the dumb stuff with inputs.
  113.  
  114. ```js
  115. class PizzaCalculator extends Component {
  116. updateNumberOfPeople = (event) => {
  117. const value = parseInt(event.target.value, 10);
  118. this.props.updateNumberOfPeople(value);
  119. }
  120.  
  121. updateSlicesPerPerson = (event) => {
  122. const value = parseInt(event.target.value, 10);
  123. this.props.updateSlicesPerPerson(value);
  124. }
  125.  
  126. reset = () => {
  127. this.props.reset();
  128. }
  129.  
  130. render() {
  131. const {
  132. numberOfPeople,
  133. slicesPerPerson,
  134. reset,
  135. } = this.props;
  136.  
  137. const numberOfPizzas = calculatePizzasNeeded(
  138. numberOfPeople,
  139. slicesPerPerson,
  140. );
  141.  
  142. return (
  143. <div className="Application">
  144. <Title />
  145. <Input
  146. label="Number of Guests"
  147. type="number"
  148. min={0}
  149. value={numberOfPeople}
  150. onChange={this.updateNumberOfPeople}
  151. />
  152. <Input
  153. label="Slices Per Person"
  154. type="number"
  155. min={0}
  156. value={slicesPerPerson}
  157. onChange={this.updateSlicesPerPerson}
  158. />
  159. <Result amount={numberOfPizzas} />
  160. <button className="full-width" onClick={reset}>
  161. Reset
  162. </button>
  163. </div>
  164. );
  165. }
  166. }
  167. ```
Add Comment
Please, Sign In to add comment