@etsoo/react
Version:
TypeScript ReactJs UI Independent Framework
32 lines (31 loc) • 792 B
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
import React from "react";
import { useLocation } from "react-router";
import { useWindowScroll } from "../uses/useWindowScroll";
const urls = {};
/**
* Scroll restoration
*/
export function ScrollRestoration() {
// Location key
const { key } = useLocation();
// Mounted
const mounted = React.useRef(false);
// Detect scroll
const data = useWindowScroll();
if (mounted.current) {
urls[key] = data;
}
// Setup
React.useEffect(() => {
const pos = urls[key];
if (pos) {
window.scrollTo(pos.x, pos.y);
}
mounted.current = true;
return () => {
mounted.current = false;
};
}, [key]);
return _jsx(React.Fragment, {});
}