From 046ec0f229dd2fec80a65768e3167d90075d104a Mon Sep 17 00:00:00 2001 From: Hunter Wilhelm Date: Thu, 1 Dec 2022 18:45:10 -0700 Subject: [PATCH 1/4] Fix use-hash extra rerendering issue Two bugs fixed: * If you `setHash` to something that doesn't start with "#" it would be reflect that temporarily in `hash` * `setHashValue` was being called multiple times (one in the listener and the set hash) resulting in the multiple renders Result: * `setHash` only rerenders the page once. --- src/mantine-hooks/src/use-hash/use-hash.ts | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/mantine-hooks/src/use-hash/use-hash.ts b/src/mantine-hooks/src/use-hash/use-hash.ts index 264dfb04bdb..8fbdcfcf6a5 100644 --- a/src/mantine-hooks/src/use-hash/use-hash.ts +++ b/src/mantine-hooks/src/use-hash/use-hash.ts @@ -1,21 +1,23 @@ -import { useState, useEffect } from 'react'; +import { useState } from 'react'; import { useWindowEvent } from '../use-window-event/use-window-event'; export function useHash() { - const [hash, setHashValue] = useState(''); + const [hash, setHashValue] = useState(() => window.location.hash); const setHash = (value: string) => { + if (!value.startsWith("#")) { + value = "#" + value; + } window.location.hash = value; setHashValue(value); }; - useWindowEvent('hashchange', () => { - setHashValue(window.location.hash); + useWindowEvent("hashchange", () => { + const newHash = window.location.hash; + if (hash !== newHash) { + setHashValue(hash); + } }); - useEffect(() => { - setHash(window.location.hash); - }, []); - return [hash, setHash] as const; } From e1d3e5f65346bc287892e052aabc03da4b077448 Mon Sep 17 00:00:00 2001 From: Hunter Wilhelm Date: Fri, 2 Dec 2022 10:58:18 -0700 Subject: [PATCH 2/4] Fix tests window is not defined in ssr --- src/mantine-hooks/src/use-hash/use-hash.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/mantine-hooks/src/use-hash/use-hash.ts b/src/mantine-hooks/src/use-hash/use-hash.ts index 8fbdcfcf6a5..2934e11db43 100644 --- a/src/mantine-hooks/src/use-hash/use-hash.ts +++ b/src/mantine-hooks/src/use-hash/use-hash.ts @@ -1,8 +1,9 @@ -import { useState } from 'react'; +import { useState, useEffect } from "react"; import { useWindowEvent } from '../use-window-event/use-window-event'; + export function useHash() { - const [hash, setHashValue] = useState(() => window.location.hash); + const [hash, setHashValue] = useState(""); const setHash = (value: string) => { if (!value.startsWith("#")) { @@ -19,5 +20,9 @@ export function useHash() { } }); + useEffect(() => { + setHashValue(window.location.hash); + }, []); + return [hash, setHash] as const; } From 466a9bc46c5f41bbb4fbafe4ec4c9ff3a25b775c Mon Sep 17 00:00:00 2001 From: hunterwilhelm Date: Fri, 2 Dec 2022 11:17:06 -0700 Subject: [PATCH 3/4] fix formatting --- src/mantine-hooks/src/use-hash/use-hash.ts | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/mantine-hooks/src/use-hash/use-hash.ts b/src/mantine-hooks/src/use-hash/use-hash.ts index 2934e11db43..f4decd2afd9 100644 --- a/src/mantine-hooks/src/use-hash/use-hash.ts +++ b/src/mantine-hooks/src/use-hash/use-hash.ts @@ -1,19 +1,18 @@ -import { useState, useEffect } from "react"; +import { useState, useEffect } from 'react'; import { useWindowEvent } from '../use-window-event/use-window-event'; - export function useHash() { - const [hash, setHashValue] = useState(""); + const [hash, setHashValue] = useState(''); const setHash = (value: string) => { - if (!value.startsWith("#")) { - value = "#" + value; + if (!value.startsWith('#')) { + value = '#' + value; } window.location.hash = value; setHashValue(value); }; - useWindowEvent("hashchange", () => { + useWindowEvent('hashchange', () => { const newHash = window.location.hash; if (hash !== newHash) { setHashValue(hash); From 7d7f44c52ca1d70ba5bb0d6f4b9efb7e2561ef84 Mon Sep 17 00:00:00 2001 From: hunterwilhelm Date: Fri, 2 Dec 2022 13:26:41 -0700 Subject: [PATCH 4/4] Fix no-param-reassign and prefer-template --- src/mantine-hooks/src/use-hash/use-hash.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/mantine-hooks/src/use-hash/use-hash.ts b/src/mantine-hooks/src/use-hash/use-hash.ts index f4decd2afd9..4436f269bdc 100644 --- a/src/mantine-hooks/src/use-hash/use-hash.ts +++ b/src/mantine-hooks/src/use-hash/use-hash.ts @@ -5,11 +5,9 @@ export function useHash() { const [hash, setHashValue] = useState(''); const setHash = (value: string) => { - if (!value.startsWith('#')) { - value = '#' + value; - } - window.location.hash = value; - setHashValue(value); + const valueWithHash = value.startsWith('#') ? value : `#${value}`; + window.location.hash = valueWithHash; + setHashValue(valueWithHash); }; useWindowEvent('hashchange', () => {