Skip to content

Instantly share code, notes, and snippets.

@Jak-Ch-ll
Jak-Ch-ll / usePreserveScroll.tsx
Last active January 22, 2025 21:03
Next.js - Preserve Scroll History
import { useRouter } from "next/router"
import { useEffect, useRef } from "react"
export const usePreserveScroll = () => {
const router = useRouter()
const scrollPositions = useRef<{ [url: string]: number }>({})
const isBack = useRef(false)
useEffect(() => {