From 817816828c2e5913115901f6a2e489ebbe076b67 Mon Sep 17 00:00:00 2001 From: MoeCasts <37169906+MoeCasts@users.noreply.github.com> Date: Fri, 5 Feb 2021 20:39:22 +0800 Subject: [PATCH 1/2] fix: useLayoutEffect will be warning in ssr mode --- packages/plugin-locale/src/templates/locale.tpl | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/plugin-locale/src/templates/locale.tpl b/packages/plugin-locale/src/templates/locale.tpl index 13159bf8f..6a9c1dbe5 100644 --- a/packages/plugin-locale/src/templates/locale.tpl +++ b/packages/plugin-locale/src/templates/locale.tpl @@ -27,6 +27,13 @@ export function _onCreate() { setIntl(locale); } +const useIsomorphicLayoutEffect = + typeof window !== 'undefined' && + typeof window.document !== 'undefined' && + typeof window.document.createElement !== 'undefined' + ? React.useLayoutEffect + : React.useEffect + export const _LocaleContainer = (props:any) => { const [locale, setLocale] = React.useState(() => getLocale()); const [intl, setContainerIntl] = React.useState(() => getIntl(locale, true)); @@ -41,7 +48,7 @@ export const _LocaleContainer = (props:any) => { setContainerIntl(getIntl(locale)); }; - React.useLayoutEffect(() => { + useIsomorphicLayoutEffect(() => { event.on(LANG_CHANGE_EVENT, handleLangChange); {{#Title}} // avoid reset route title From d3fb98bfbcff9c9a91123c7fba660b2e514e23a2 Mon Sep 17 00:00:00 2001 From: MoeCasts <37169906+MoeCasts@users.noreply.github.com> Date: Sat, 6 Feb 2021 01:08:09 +0800 Subject: [PATCH 2/2] format the code --- packages/plugin-locale/src/templates/locale.tpl | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/plugin-locale/src/templates/locale.tpl b/packages/plugin-locale/src/templates/locale.tpl index 6a9c1dbe5..5f7c69dce 100644 --- a/packages/plugin-locale/src/templates/locale.tpl +++ b/packages/plugin-locale/src/templates/locale.tpl @@ -27,12 +27,12 @@ export function _onCreate() { setIntl(locale); } -const useIsomorphicLayoutEffect = - typeof window !== 'undefined' && - typeof window.document !== 'undefined' && - typeof window.document.createElement !== 'undefined' - ? React.useLayoutEffect - : React.useEffect +const useIsomorphicLayoutEffect = + typeof window !== 'undefined' && + typeof window.document !== 'undefined' && + typeof window.document.createElement !== 'undefined' + ? React.useLayoutEffect + : React.useEffect export const _LocaleContainer = (props:any) => { const [locale, setLocale] = React.useState(() => getLocale());