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

[@mantine/hooks] use-timeout: Memoize returned callbacks #4914

Merged
merged 3 commits into from
Oct 1, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions src/mantine-hooks/src/use-timeout/use-timeout.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useRef, useEffect } from 'react';
import { useRef, useEffect, useCallback } from 'react';

export function useTimeout(
callback: (...callbackParams: any[]) => void,
Expand All @@ -7,29 +7,32 @@ export function useTimeout(
) {
const timeoutRef = useRef<number | null>(null);

const start = (...callbackParams: any[]) => {
if (!timeoutRef.current) {
timeoutRef.current = window.setTimeout(() => {
callback(callbackParams);
timeoutRef.current = null;
}, delay);
}
};
const start = useCallback(
(...callbackParams: any[]) => {
if (!timeoutRef.current) {
timeoutRef.current = window.setTimeout(() => {
callback(callbackParams);
timeoutRef.current = null;
}, delay);
}
},
[callback, delay],
);

const clear = () => {
const clear = useCallback(() => {
if (timeoutRef.current) {
window.clearTimeout(timeoutRef.current);
timeoutRef.current = null;
}
};
}, []);

useEffect(() => {
if (options.autoInvoke) {
start();
}

return clear;
}, [delay]);
}, [clear, delay, options.autoInvoke, start]);
Copy link
Member

Choose a reason for hiding this comment

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

There is no reason to add options.autoInvoke to the dependencies array. start should be called exactly once when hook is initialized.

Copy link
Member

Choose a reason for hiding this comment

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

@Raicuparta you did not resolve my comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do we really want an effect here then?

The way it was before, changing the delay would also autoInvoke again, but changing the callback wouldn't. Was that intended? Because it seems very counterintuitive to me.

In this PR, changing the callback, the delay, or the autoInvoke option would trigger the autoInvoke again.

If the intention is to really only make it run exactly once, we should just have no dependencies at all, but then maybe it shouldn't be in an effect at all.

I have removed autoInvoke from the dependencies anyway to avoid making changes outside the scope of the PR, but this is pretty clearly against the intended useEffect usage as per the docs: https://react.dev/learn/removing-effect-dependencies.

(I also removed delay from the dependencies since it is now redundant)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh and yeah I didn't mean to make that first push that only removed the delay, sorry about the notification spam


return { start, clear };
}