From 4082143e1032410d5f89891dbb363a64a823fce4 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Fri, 3 Sep 2021 19:07:53 +0300 Subject: [PATCH] Fix unexpected history back navigations (#1424) --- src/hooks/useHistoryBack.ts | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/hooks/useHistoryBack.ts b/src/hooks/useHistoryBack.ts index 9934b17fc..9b82a0767 100644 --- a/src/hooks/useHistoryBack.ts +++ b/src/hooks/useHistoryBack.ts @@ -124,18 +124,20 @@ export default function useHistoryBack( } else { const prev = prevHashes || []; if (prev.length < hashes.length) { - const index = ++historyState.currentIndex; - historyState.currentIndexes.push(index); + setTimeout(() => { + const index = ++historyState.currentIndex; + historyState.currentIndexes.push(index); - window.history.pushState({ - index, - state: currentState, - }, '', `#${hashes[hashes.length - 1]}`); + window.history.pushState({ + index, + state: currentState, + }, '', `#${hashes[hashes.length - 1]}`); - indexHashRef.current.push({ - index, - hash: hashes[hashes.length - 1], - }); + indexHashRef.current.push({ + index, + hash: hashes[hashes.length - 1], + }); + }, 0); } else { const delta = prev.length - hashes.length; if (isHashChangedFromEvent.current) {