Skip to content

Commit 5385031

Browse files
authored
[Beta] Fix scroll jumps on navigating to fragments (reactjs#5375)
1 parent c4d2a6d commit 5385031

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

Diff for: beta/src/components/MDX/ExpandableExample.tsx

+5-8
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,17 @@ function ExpandableExample({children, excerpt, type}: ExpandableExampleProps) {
2828
const isExample = type === 'Example';
2929
const id = children[0].props.id;
3030

31-
const queuedExpandRef = useRef<boolean>(true);
3231
const {asPath} = useRouter();
33-
// init as expanded to prevent flash
34-
const [isExpanded, setIsExpanded] = useState(true);
32+
const shouldAutoExpand = id === asPath.split('#')[1];
33+
const queuedExpandRef = useRef<boolean>(shouldAutoExpand);
34+
const [isExpanded, setIsExpanded] = useState(false);
3535

36-
// asPath would mismatch between server and client, reset here instead of put it into init state
3736
useEffect(() => {
3837
if (queuedExpandRef.current) {
3938
queuedExpandRef.current = false;
40-
if (id !== asPath.split('#')[1]) {
41-
setIsExpanded(false);
42-
}
39+
setIsExpanded(true);
4340
}
44-
}, [asPath, id]);
41+
}, []);
4542

4643
return (
4744
<details

0 commit comments

Comments
 (0)