Browse Source

docs: fix hydration error when resizing (#41370)

* docs: fix hydration error when resizing

* docs: useLayoutState
pull/41372/head
MadCcc 2 years ago
committed by GitHub
parent
commit
772ab49baf
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 17
      .dumi/hooks/useLayoutState.ts
  2. 15
      .dumi/theme/layouts/GlobalLayout.tsx
  3. 2
      package.json

17
.dumi/hooks/useLayoutState.ts

@ -0,0 +1,17 @@
import { startTransition, useState } from 'react';
const useLayoutState = <S>(
...args: Parameters<typeof useState<S>>
): ReturnType<typeof useState<S>> => {
const [state, setState] = useState<S>(...args);
const setLayoutState: typeof setState = (...setStateArgs) => {
startTransition(() => {
setState(...setStateArgs);
});
};
return [state, setLayoutState];
};
export default useLayoutState;

15
.dumi/theme/layouts/GlobalLayout.tsx

@ -5,15 +5,16 @@ import {
parentSelectorLinter, parentSelectorLinter,
StyleProvider, StyleProvider,
} from '@ant-design/cssinjs'; } from '@ant-design/cssinjs';
import { ConfigProvider, theme as antdTheme, App } from 'antd'; import { App, ConfigProvider, theme as antdTheme } from 'antd';
import type { DirectionType } from 'antd/es/config-provider'; import type { DirectionType } from 'antd/es/config-provider';
import { createSearchParams, useOutlet, useSearchParams } from 'dumi'; import { createSearchParams, useOutlet, useSearchParams } from 'dumi';
import React, { startTransition, useCallback, useEffect, useMemo } from 'react'; import React, { useCallback, useEffect, useMemo } from 'react';
import useLocation from '../../hooks/useLocation'; import useLocation from '../../hooks/useLocation';
import type { ThemeName } from '../common/ThemeSwitch'; import type { ThemeName } from '../common/ThemeSwitch';
import ThemeSwitch from '../common/ThemeSwitch'; import ThemeSwitch from '../common/ThemeSwitch';
import type { SiteContextProps } from '../slots/SiteContext'; import type { SiteContextProps } from '../slots/SiteContext';
import SiteContext from '../slots/SiteContext'; import SiteContext from '../slots/SiteContext';
import useLayoutState from '../../hooks/useLayoutState';
type Entries<T> = { [K in keyof T]: [K, T[K]] }[keyof T][]; type Entries<T> = { [K in keyof T]: [K, T[K]] }[keyof T][];
type SiteState = Partial<Omit<SiteContextProps, 'updateSiteContext'>>; type SiteState = Partial<Omit<SiteContextProps, 'updateSiteContext'>>;
@ -40,7 +41,7 @@ const GlobalLayout: React.FC = () => {
const outlet = useOutlet(); const outlet = useOutlet();
const { pathname } = useLocation(); const { pathname } = useLocation();
const [searchParams, setSearchParams] = useSearchParams(); const [searchParams, setSearchParams] = useSearchParams();
const [{ theme, direction, isMobile }, setSiteState] = React.useState<SiteState>({ const [{ theme, direction, isMobile }, setSiteState] = useLayoutState<SiteState>({
isMobile: false, isMobile: false,
direction: 'ltr', direction: 'ltr',
theme: ['light'], theme: ['light'],
@ -85,11 +86,9 @@ const GlobalLayout: React.FC = () => {
const _theme = searchParams.getAll('theme') as ThemeName[]; const _theme = searchParams.getAll('theme') as ThemeName[];
const _direction = searchParams.get('direction') as DirectionType; const _direction = searchParams.get('direction') as DirectionType;
startTransition(() => { setSiteState({ theme: _theme, direction: _direction === 'rtl' ? 'rtl' : 'ltr' });
setSiteState({ theme: _theme, direction: _direction === 'rtl' ? 'rtl' : 'ltr' }); // Handle isMobile
// Handle isMobile updateMobileMode();
updateMobileMode();
});
window.addEventListener('resize', updateMobileMode); window.addEventListener('resize', updateMobileMode);
return () => { return () => {

2
package.json

@ -203,7 +203,7 @@
"cheerio": "1.0.0-rc.12", "cheerio": "1.0.0-rc.12",
"cross-env": "^7.0.0", "cross-env": "^7.0.0",
"dekko": "^0.2.1", "dekko": "^0.2.1",
"dumi": "^2.1.13", "dumi": "^2.1.17",
"duplicate-package-checker-webpack-plugin": "^3.0.0", "duplicate-package-checker-webpack-plugin": "^3.0.0",
"esbuild-loader": "^3.0.0", "esbuild-loader": "^3.0.0",
"eslint": "^8.0.0", "eslint": "^8.0.0",

Loading…
Cancel
Save