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

Unavoidable eslintreact-hooks/exhaustive-deps warning after restoring scroll position from session storage #22414

Closed
piotrpawlik opened this issue Sep 24, 2021 · 2 comments
Labels
Component: ESLint Rules Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug

Comments

@piotrpawlik
Copy link

My implementation for restoring scroll position after route change goes like this:

import { useEffect, useRef } from 'react'
import { useLocation } from 'react-router-dom'
import { useScroll, useSessionStorage } from 'react-use'

export const useRestoreScrollOnRouteChange = (storageKey: string) => {
  const ref = useRef<HTMLDivElement>(null)
  const { y } = useScroll(ref)
  const { pathname } = useLocation()
  const scrollPositionRestoredRef = useRef(false)
  const [storedScroll, storeScroll] = useSessionStorage<number>(storageKey, 0)

  useEffect(() => {
    ref.current?.scrollTo({ top: storedScroll })
    scrollPositionRestoredRef.current = true
  }, [pathname]) // scroll position restoration should happen only on route change - eslintreact-hooks/exhaustive-deps warns about this line

  useEffect(() => {
    if (scrollPositionRestoredRef.current) {
      storeScroll(y)
    }
  }, [y])

  return ref
}

I had to omit storedScroll from dependencies of first useEffect since I do want "restoring" to be triggered only when route changes.

I went through entire https://reactjs.org/docs/hooks-faq.html#is-it-safe-to-omit-functions-from-the-list-of-dependencies section and got encourage by docs to create this issue although I don't see any alternative implementation that wouldn't fire warning.

At the end, storedScroll is dependent on sessionStorage not state nor props (although internally in useSessionStorage it is stored as state).

@marko-knoebl
Copy link

At the end, storedScroll is dependent on sessionStorage not state nor props (although internally in useSessionStorage it is stored as state).

I think that's the root cause of the issue here - you're using a React state internally where you wouldn't need one.

I think you could just replace:

storeScroll(y) -> sessionStorage.setItem(storageKey, y)

and inside your first effect: const storedScroll = Number(sessionStorage.getItem(storageKey))

Then you won't need the extra hook

@eps1lon eps1lon added Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug Component: ESLint Rules labels May 17, 2022
@eps1lon
Copy link
Collaborator

eps1lon commented May 17, 2022

Closing in favor of #15240 which talks about the same underlying issue.

There was an RFC recently published that might help with this issue: reactjs/rfcs#220

@eps1lon eps1lon closed this as completed May 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: ESLint Rules Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug
Projects
None yet
Development

No branches or pull requests

3 participants