Skip to content
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

feat(@toss/react): useInterval stop/resume support #476

Merged
merged 3 commits into from
Jun 3, 2024

Conversation

d0422
Copy link
Contributor

@d0422 d0422 commented May 14, 2024

Overview

When using Interval, sometimes you need to stop and resume the interval.

When configuring the carousel, if auto-turning is implemented through useInterval, the interval must be stopped when the user is in the middle of the slide, and the interval must be resumed when the slide is completed.

In this case, I suggest that if it returns the factors presented above in the hook, it will be easy to implement!

Accordingly, why don't you return the stop, resume function, and intervalRunning status.

PR Checklist

  • I read and included theses actions below
  1. I have read the Contributing Guide
  2. I have written documents and tests, if needed.

Copy link

vercel bot commented May 14, 2024

@d0422 is attempting to deploy a commit to the Toss Team on Vercel.

A member of the Team first needs to authorize it.

@raon0211
Copy link
Collaborator

raon0211 commented Jun 1, 2024

Hello, thanks for your contribution!

While this change helps the use of useInterval, I think introducing a state within this hook is not a preferable choice for us (since adds a new state for all users of the hook who doesn't have the need to start or stop the interval).

However, I think adding an enabled option like useInterval(fn, { delay: 1000, enabled: false }) might be a good design choice, since it does not add a state to our hook.

What do you think?

@d0422
Copy link
Contributor Author

d0422 commented Jun 2, 2024

@raon0211
Thank you for your response! I'm honored to contribute.
I agree that adding useState might not be beneficial for all users.
I think it would be better to reflect the design you presented.

type IntervalOptions =
  | number
  | {
      delay: number | null;
      trailing?: boolean;
      enabled?: boolean;
    };

}

const id = window.setInterval(tick, delay);
return () => window.clearInterval(id);
}, [delay, savedCallback]);
}, [delay, savedCallback, enabled]);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it would be better for us if we did not register a callback at all if it is not enabled like the following code.

useEffect(() => {
  if (delay == null || !enabled) {
    return;
  }

  const id = window.setInterval(tick, delay);
  return () => window.clearInterval(id);
}, [delay, savedCallback, enabled]);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for your feedback. I have reflected your opinion and added the corresponding test code.

@raon0211 raon0211 merged commit 17e900d into toss:main Jun 3, 2024
5 of 6 checks passed
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.

2 participants