From 53c0e0d24080735d4778428f76e943148749c193 Mon Sep 17 00:00:00 2001 From: Arnaud Touillet <49291068+Jacouille@users.noreply.github.com> Date: Tue, 3 Oct 2023 19:38:06 +0000 Subject: [PATCH 1/2] [@mantine/hooks] useTimeout: callback not running after state change --- src/mantine-hooks/src/use-timeout/use-timeout.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mantine-hooks/src/use-timeout/use-timeout.ts b/src/mantine-hooks/src/use-timeout/use-timeout.ts index 46f94f50a85..44696a60efe 100644 --- a/src/mantine-hooks/src/use-timeout/use-timeout.ts +++ b/src/mantine-hooks/src/use-timeout/use-timeout.ts @@ -16,7 +16,7 @@ export function useTimeout( }, delay); } }, - [callback, delay] + [delay] ); const clear = useCallback(() => { From 201c34e9e30991772daa82c137bd8220e24574d2 Mon Sep 17 00:00:00 2001 From: Arnaud Touillet <49291068+Jacouille@users.noreply.github.com> Date: Tue, 3 Oct 2023 20:06:20 +0000 Subject: [PATCH 2/2] [core] Added unit test to cover use case --- docs/yarn.lock | 8 ++++---- .../src/use-timeout/use-timeout.test.ts | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/docs/yarn.lock b/docs/yarn.lock index 532d7abd7ff..f8894d90519 100644 --- a/docs/yarn.lock +++ b/docs/yarn.lock @@ -133,10 +133,10 @@ version "0.0.0" uid "" -"@mantine/store@7.1.1": - version "7.1.1" - resolved "https://registry.yarnpkg.com/@mantine/store/-/store-7.1.1.tgz#859071535c97fc54862ffe946f58d95cb0455777" - integrity sha512-gCF59GbcRdAOqhrjWQczCAL3Hg+gMaiuATkXFCgbjkwIYXy22zlFAnSKckKHxWuRu7d51dJkPrGelnc7HtZQzA== +"@mantine/store@7.1.2": + version "7.1.2" + resolved "https://registry.yarnpkg.com/@mantine/store/-/store-7.1.2.tgz#bbdf9b7e9f1f97978100868c1a705e4bc0ac7e74" + integrity sha512-Lf3FLymM0q92BuRC4tZxTxrb9EjVa+J8fqEV147u/Q3aUSNmkhJCqN2MXPbTHIBJ2PsbLtDhy/2edNyIK1KhKQ== "@mantine/store@link:../src/mantine-store": version "0.0.0" diff --git a/src/mantine-hooks/src/use-timeout/use-timeout.test.ts b/src/mantine-hooks/src/use-timeout/use-timeout.test.ts index cae2140d8af..0be06daeec6 100644 --- a/src/mantine-hooks/src/use-timeout/use-timeout.test.ts +++ b/src/mantine-hooks/src/use-timeout/use-timeout.test.ts @@ -70,6 +70,20 @@ describe('@mantine/hooks/use-timeout', () => { expect(setTimeout).toHaveBeenCalled(); }); + it('callback should be called when rerender is triggered before timeout', () => { + const { timeout, advanceTimerToNextTick } = setupTimer(); + const { rerender, result } = renderHook(() => useTimeout(callback, timeout)); + + result.current.start(); + + rerender(); + + advanceTimerToNextTick(); + + expect(callback).toHaveBeenCalled(); + expect(setTimeout).toHaveBeenCalled(); + }); + it('timeout is cleared on calling clear', () => { const { timeout, advanceTimerToNextTick } = setupTimer(10); const hook = renderHook(() => useTimeout(callback, timeout, { autoInvoke: false }));