Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. import React, { useContext, useState } from 'react';
  2.  
  3. import { CounterContext } from '../context/CounterContext';
  4. import LoadingText from './atoms/LoadingText';
  5.  
  6. const Counter = () => {
  7. const [counter, actions] = useContext(CounterContext);
  8. const [loading, setLoading] = useState({
  9. up: false,
  10. down: false,
  11. reset: false
  12. });
  13.  
  14. const handleAction = action => {
  15. if (action === actions.up) {
  16. setLoading(prev => ({ ...prev, up: true }));
  17. action().finally(() => setLoading(prev => ({ ...prev, up: false })));
  18. }
  19. if (action === actions.down) {
  20. setLoading(prev => ({ ...prev, down: true }));
  21. action().finally(() => setLoading(prev => ({ ...prev, down: false })));
  22. }
  23. if (action === actions.reset) {
  24. setLoading(prev => ({ ...prev, reset: true }));
  25. action().finally(() => setLoading(prev => ({ ...prev, reset: false })));
  26. }
  27. };
  28.  
  29. return (
  30. <React.Fragment>
  31. <h3>Count: {counter.count}</h3>
  32. <button onClick={() => handleAction(actions.up)}>
  33. <LoadingText isLoading={loading.up}>Up</LoadingText>
  34. </button>
  35. <button onClick={() => handleAction(actions.down)}>
  36. <LoadingText isLoading={loading.down}>Down</LoadingText>
  37. </button>
  38. <button onClick={() => handleAction(actions.reset)}>
  39. <LoadingText isLoading={loading.reset}>Reset</LoadingText>
  40. </button>
  41. </React.Fragment>
  42. );
  43. };
  44.  
  45. export default Counter;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement