Skip to content
New issue

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

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

Already on GitHub? # to your account

feat: add useInterval and useTimeout #1462

Merged
merged 3 commits into from
Nov 8, 2019
Merged

feat: add useInterval and useTimeout #1462

merged 3 commits into from
Nov 8, 2019

Conversation

yuanyan
Copy link
Collaborator

@yuanyan yuanyan commented Nov 4, 2019

Why useInterval? setInterval will called even component is unmounted that make error happens, and useInterval will auto clearInterval before component will mount.

import { createElement } from 'rax';
import useInterval from 'rax-use-interval';

function Example() {
  const [count, setCount] = useState(0);
  useInterval(() => {
    setCount(count + 1);
  }, 1000);

  return <h1>{count}</h1>;
}


function clearTimer(id) {
if (id != null) clearInterval(id);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

其实这里可以不用判断, clearInterval 接受任何类型都不会抛出异常

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

  1. 逻辑上正确 2. clearInterval 还是比较重的行为

import { useEffect, useRef } from 'rax';

function clearTimer(id) {
if (id != null) clearTimeout(id);
Copy link
Collaborator

Choose a reason for hiding this comment

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

同上

Copy link
Collaborator

@wssgcg1213 wssgcg1213 left a comment

Choose a reason for hiding this comment

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

其它没有问题

@yuanyan yuanyan merged commit 8258654 into master Nov 8, 2019
@yuanyan yuanyan deleted the feat-use-timeout branch November 8, 2019 07:14
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants