Counter.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import React, { useState } from 'react';
  2. import { useSelector, useDispatch } from 'react-redux';
  3. import {
  4. decrement,
  5. increment,
  6. incrementByAmount,
  7. incrementAsync,
  8. incrementIfOdd,
  9. selectCount,
  10. } from './counterSlice';
  11. import styles from './Counter.module.css';
  12. export function Counter() {
  13. const count = useSelector(selectCount);
  14. const dispatch = useDispatch();
  15. const [incrementAmount, setIncrementAmount] = useState('2');
  16. const incrementValue = Number(incrementAmount) || 0;
  17. return (
  18. <div>
  19. <div className={styles.row}>
  20. <button
  21. className={styles.button}
  22. aria-label="Decrement value"
  23. onClick={() => dispatch(decrement())}
  24. >
  25. -
  26. </button>
  27. <span className={styles.value}>{count}</span>
  28. <button
  29. className={styles.button}
  30. aria-label="Increment value"
  31. onClick={() => dispatch(increment())}
  32. >
  33. +
  34. </button>
  35. </div>
  36. <div className={styles.row}>
  37. <input
  38. className={styles.textbox}
  39. aria-label="Set increment amount"
  40. value={incrementAmount}
  41. onChange={(e) => setIncrementAmount(e.target.value)}
  42. />
  43. <button
  44. className={styles.button}
  45. onClick={() => dispatch(incrementByAmount(incrementValue))}
  46. >
  47. Add Amount
  48. </button>
  49. <button
  50. className={styles.asyncButton}
  51. onClick={() => dispatch(incrementAsync(incrementValue))}
  52. >
  53. Add Async
  54. </button>
  55. <button
  56. className={styles.button}
  57. onClick={() => dispatch(incrementIfOdd(incrementValue))}
  58. >
  59. Add If Odd
  60. </button>
  61. </div>
  62. </div>
  63. );
  64. }