Skip to content

Commit 3f7e05d

Browse files
committed
Tweak wording
1 parent fac5525 commit 3f7e05d

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/content/reference/react/Activity.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ While hidden, children still re-render in response to new props, albeit at a low
5151

5252
When the boundary becomes <CodeStep step={3}>visible</CodeStep> again, React will reveal the children with their previous state restored, and create their Effects.
5353

54-
In this way, Activity can thought of as a mechanism for rendering "background activity". Rather than unmounting content that's likely to become visible again, you can use Activity to maintain and restore that content's UI and internal state.
54+
In this way, Activity can thought of as a mechanism for rendering "background activity". Rather than completely discarding content that's likely to become visible again, you can use Activity to maintain and restore that content's UI and internal state.
5555

5656
[See more examples below.](#usage)
5757

src/pages/[[...markdownPath]].js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,34 @@ import {MDXComponents} from 'components/MDX/MDXComponents';
1414
import compileMDX from 'utils/compileMDX';
1515
import {generateRssFeed} from '../utils/rss';
1616

17+
import {useEffect} from 'react';
18+
19+
export function useScrollRestoration(key) {
20+
// Restore scroll position on page load
21+
useEffect(() => {
22+
const saved = sessionStorage.getItem(key);
23+
if (saved) {
24+
const {x, y} = JSON.parse(saved);
25+
window.scrollTo(x, y);
26+
}
27+
}, [key]);
28+
29+
// Save scroll position on unload
30+
useEffect(() => {
31+
const save = () => {
32+
sessionStorage.setItem(
33+
key,
34+
JSON.stringify({x: window.scrollX, y: window.scrollY})
35+
);
36+
};
37+
38+
window.addEventListener('beforeunload', save);
39+
return () => window.removeEventListener('beforeunload', save);
40+
}, [key]);
41+
}
42+
1743
export default function Layout({content, toc, meta, languages}) {
44+
useScrollRestoration();
1845
const parsedContent = useMemo(
1946
() => JSON.parse(content, reviveNodeOnClient),
2047
[content]

0 commit comments

Comments
 (0)