Skip to content

Commit e1b4da0

Browse files
authored
fix: restore duplicate Tabs rendering to fix hydration issue (facebook#5652)
* restore duplicate Tabs rendering to fix hydration issue * comment
1 parent 03eda3e commit e1b4da0

2 files changed

Lines changed: 35 additions & 2 deletions

File tree

packages/docusaurus-theme-classic/src/theme/Tabs/index.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
import React, {useState, cloneElement, Children, ReactElement} from 'react';
9+
import useIsBrowser from '@docusaurus/useIsBrowser';
910
import useUserPreferencesContext from '@theme/hooks/useUserPreferencesContext';
1011
import type {Props} from '@theme/Tabs';
1112
import type {Props as TabItemProps} from '@theme/TabItem';
@@ -21,7 +22,7 @@ function isInViewport(element: HTMLElement): boolean {
2122
return top >= 0 && right <= innerWidth && bottom <= innerHeight && left >= 0;
2223
}
2324

24-
function Tabs(props: Props): JSX.Element {
25+
function TabsComponent(props: Props): JSX.Element {
2526
const {
2627
lazy,
2728
block,
@@ -163,4 +164,14 @@ function Tabs(props: Props): JSX.Element {
163164
);
164165
}
165166

166-
export default Tabs;
167+
export default function Tabs(props: Props): JSX.Element {
168+
const isBrowser = useIsBrowser();
169+
return (
170+
<TabsComponent
171+
// Remount tabs after hydration
172+
// Temporary fix for https://github.com/facebook/docusaurus/issues/5653
173+
key={String(isBrowser)}
174+
{...props}
175+
/>
176+
);
177+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Tabs tests
2+
3+
```mdx-code-block
4+
import BrowserWindow from '@site/src/components/BrowserWindow';
5+
import Tabs from '@theme/Tabs';
6+
import TabItem from '@theme/TabItem';
7+
```
8+
9+
## Tabs with dynamic default value
10+
11+
This can cause [bugs](https://github.com/facebook/react-native-website/issues/2771) when default value is different between SSR and client:
12+
13+
```mdx-code-block
14+
export const isMacOS = typeof window !== 'undefined' && navigator.platform.startsWith('Mac');
15+
16+
<BrowserWindow>
17+
<Tabs defaultValue={isMacOS ? "ios" : "android"}>
18+
<TabItem value="android" label="Android">Android content</TabItem>
19+
<TabItem value="ios" label="iOS">iOS content</TabItem>
20+
</Tabs>
21+
</BrowserWindow>
22+
```

0 commit comments

Comments
 (0)