1. import React, { useState, useRef, useCallback } from "react";
    2. // null means pause
    3. // 1000 means 1 sec
    4. type DelayType = null | 1000;
    5. // 🔴 Finish this function
    6. function useIncreasingCount() {
    7. const [count, setCount] = useState<number>(0);
    8. const [delay, setDelay] = React.useState<DelayType>(null);
    9. const delayRef = useRef<number>(null);
    10. const toggleDelay = useCallback(() => {
    11. clearInterval(delayRef.current);
    12. if (delay) {
    13. setDelay(null);
    14. } else {
    15. setDelay(1000);
    16. delayRef.current = setInterval(() => {
    17. setCount((count: number) => count + 1);
    18. }, 1000);
    19. }
    20. }, [delay]);
    21. return {
    22. count,
    23. delay,
    24. toggleDelay,
    25. };
    26. }
    27. export default function Counter() {
    28. // 🔴 Finish this function
    29. const { count, delay, toggleDelay } = useIncreasingCount();
    30. return (
    31. <div>
    32. <h1>count: {count}</h1>
    33. <button onClick={toggleDelay}>{delay ? "pause" : "play"}</button>
    34. </div>
    35. );
    36. }