Fix sessionStorage error

pull/2375/head
Jérémy Rialland 2022-11-15 10:52:10 +01:00 committed by Jérémy Rialland
parent 1aef3657c0
commit 28225b246a
1 changed files with 4 additions and 3 deletions

View File

@ -1,15 +1,16 @@
import { debounce } from '@/utils'
import { debounce, getSessionStorage } from '@/utils'
import { useEffect } from 'react'
import { useLocation, useNavigationType } from 'react-router-dom'
const POP_ACTION_LABEL = 'POP'
const sessionStorage = getSessionStorage()
export const useSaveAndRestoreScrollPosition = () => {
const location = useLocation()
const navigationType = useNavigationType()
useEffect(() => {
const scrollPosition = sessionStorage.getItem(location.pathname)
const scrollPosition = sessionStorage?.getItem(location.pathname)
if (scrollPosition && navigationType === POP_ACTION_LABEL) {
window.scrollTo(0, parseInt(scrollPosition))
@ -18,7 +19,7 @@ export const useSaveAndRestoreScrollPosition = () => {
useEffect(() => {
const saveScrollYPosition = debounce(100, () => {
sessionStorage.setItem(location.pathname, String(window.scrollY))
sessionStorage?.setItem(location.pathname, String(window.scrollY))
}) as (this: Window, ev: Event) => any
window.addEventListener('scroll', saveScrollYPosition)