-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
New hook: useSafeTimeout #19109
New hook: useSafeTimeout #19109
Conversation
export default function useSafeTimeout() { | ||
const timeouts = useRef( [] ); | ||
|
||
function clearTimeout( id ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we use useCallback
for these?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be fair, I'm not sure whether we really need this hook. I feel this is just a Higher-order that was needed because of how class components work. I feel with hooks, we don't really need it.
@youknowriad Could you elaborate? Why won't we need it? |
I feel when you use timeouts with hooks, you'll always do something like that;
The timeout and its clearance are located in the same code, the chance to forget about this and the added code here is very small while in the case of a class Component or components without hooks, it's quite complex to achieve: you need refactor to a class component and use separate methods to trigger and clear the timeouts. All these are fixed with hooks. |
Ah, you're right. I was thinking of a use case in RichText where |
There's now another example of this (timers in event handler) at #19881, though I'd agree that it's still not yet (to me) at a point to need a higher-level solution, and if anything its absence could serve as a deterrent to an anti-pattern. |
…41216) * refactor `<TokenInput>` to functional component and Typescript. * Remove findRenderedComponentWithType as it does not work with functional components. * use findRenderedDOMComponentWithTag instead of findRenderedComponentWithType * convert `<Token>` to TypeScript * type only import * convert SuggestionsList to TypeScript * Change to not use withSafeTimeout. ref: #19109 * Remove comment because tabIndex is not specified. * add type for dom-scroll-into-view * add form-token-field dir to include in tsconfig.json * refactoring types. * fix type. * refactor FormTokenField to TypeScript and Functional Component * refactor story * allow set suggestions in stories. * Use act where useEffect is relevant. * separate state * revert ts-nocheck * remove onChange prop form storybook * Update packages/components/src/form-token-field/token-input.tsx Co-authored-by: Lena Morita <lena@jaguchi.com> * use ComponentProps * add type docs * replace token-field-wrapper to tsx * add doc comment for FormTokenField * fix type docs * Update packages/components/src/form-token-field/types.ts Co-authored-by: Lena Morita <lena@jaguchi.com> * Update packages/components/src/form-token-field/types.ts Co-authored-by: Lena Morita <lena@jaguchi.com> * Update packages/components/src/form-token-field/index.tsx Co-authored-by: Lena Morita <lena@jaguchi.com> * Update packages/components/src/form-token-field/index.tsx Co-authored-by: Lena Morita <lena@jaguchi.com> * Update packages/components/src/form-token-field/index.tsx Co-authored-by: Lena Morita <lena@jaguchi.com> * change variable and function name * Fix useEffect to updateSuggestions(). * Disabled control for __experimentalValidateInput. Update type document. * add changelog * Update packages/components/src/form-token-field/index.tsx Co-authored-by: Lena Morita <lena@jaguchi.com> Co-authored-by: Lena Morita <lena@jaguchi.com>
Description
Moves the logic within
withSafeTimeout
to the new hookuseSafeTimeout
.How has this been tested?
Screenshots
Types of changes
Checklist: