Skip to content

[URH-15] useInterval 신규 #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 29, 2024
Merged

[URH-15] useInterval 신규 #28

merged 6 commits into from
Jul 29, 2024

Conversation

suhyeoonn
Copy link
Contributor

👾 Pull Request

  • 상태: 신규

1️⃣ Spec

  • 지정된 시간 간격으로 콜백 함수를 호출하는 타이머를 설정합니다.
  • ms 값이 변경될 때마다 interval이 재설정되며, 컴포넌트가 언마운트될 때 자동으로 타이머가 정리됩니다.
  • 반환된 clear 함수를 호출하여 수동으로 타이머를 중지할 수도 있습니다.

2️⃣ 변경 사항

  • 없음

3️⃣ 예시 코드

function App() {
  const [count, setCount] = useState(0)
  const [delay, setDelay] = useState(1000)

  const clear = useInterval(() => {
    setCount(count + 1);
  }, delay);

  const handleStop = () => {
    clear()    
  }

  const handleDelay = ({target}) => {
    setDelay(target.value)
  }

  return (
    <div>
      <input type="number" value={delay} onChange={handleDelay} />
      <button onClick={handleStop}>stop</button>
      <div>
        {count}
      </div>
    </div>
  );
}

4️⃣ 관련 문서 (선택 사항)

  • 없음

@Choozii Choozii requested review from bicochan, foresec and Choozii and removed request for foresec July 24, 2024 04:16
Copy link
Member

@Choozii Choozii left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😻

Copy link
Member

@bicochan bicochan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

고생하셨습니다!

*/
const useInterval = (callback: Fn, ms: number) => {
const savedCallback = useRef<Fn>(callback);
const intervalRef = useRef<number | null>(null);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const intervalRef = useRef<number | null>(null);
const intervalRef = useRef<number>(null);

[P2] useRef에는 null이 기본적으로 포함돼 있어서 타입에서 제거할 수 있을 것 같네요!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

타입에서 null을 제거하니 current 에 null 할당 시 Cannot assign to 'current' because it is a read-only property 에러가 발생해서요~!

intervalRef.current = null;

이 글을 참고해보니, null을 지정하지 않으면 RefObject<T> 타입으로 반환되기 때문에 current가 수정 불가능한 것 같습니다👀
지금처럼 타입에 null 을 지정해서 MutableRefObject<T>이 반환되도록 해야할 것 같은데, 혹시 @bicochan 님 의견은 어떠실까요?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

앗 그렇군요...! 제가 이 부분은 정확히 몰랐습니다...😅 수현 님 덕분에 TIL 했네요 ㅎㅎ

그대로 반영해주셔도 좋을 것 같습니다!

*/
const useInterval = (callback: Fn, ms: number) => {
const savedCallback = useRef<Fn>(callback);
const intervalRef = useRef<number | null>(null);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

앗 그렇군요...! 제가 이 부분은 정확히 몰랐습니다...😅 수현 님 덕분에 TIL 했네요 ㅎㅎ

그대로 반영해주셔도 좋을 것 같습니다!

@suhyeoonn suhyeoonn requested a review from Choozii July 29, 2024 12:58
Copy link
Member

@Choozii Choozii left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@suhyeoonn suhyeoonn merged commit a45cc03 into master Jul 29, 2024
3 checks passed
@suhyeoonn suhyeoonn deleted the URH-15/useInterval branch July 29, 2024 14:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants