-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(core): replace useDocusaurusContext().isClient by useIsBrows…
…er() (#5349) * extract separate useIsClient() hook * for consistency, rename to `useIsBrowser` * useless return * improve doc for BrowserOnly * update snapshot * polish
- Loading branch information
Showing
20 changed files
with
213 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import React, {ReactNode, useEffect, useState} from 'react'; | ||
|
||
// Encapsulate the logic to avoid React hydration problems | ||
// See https://www.joshwcomeau.com/react/the-perils-of-rehydration/ | ||
// On first client-side render, we need to render exactly as the server rendered | ||
// isBrowser is set to true only after a successful hydration | ||
|
||
// Note, isBrowser is not part of useDocusaurusContext() for perf reasons | ||
// Using useDocusaurusContext() (much more common need) should not trigger re-rendering after a successful hydration | ||
|
||
export const Context = React.createContext<boolean>(false); | ||
|
||
export function BrowserContextProvider({ | ||
children, | ||
}: { | ||
children: ReactNode; | ||
}): JSX.Element { | ||
const [isBrowser, setIsBrowser] = useState(false); | ||
|
||
useEffect(() => { | ||
setIsBrowser(true); | ||
}, []); | ||
|
||
return <Context.Provider value={isBrowser}>{children}</Context.Provider>; | ||
} |
35 changes: 35 additions & 0 deletions
35
packages/docusaurus/src/client/exports/docusaurusContext.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import React, {ReactNode} from 'react'; | ||
import {DocusaurusContext} from '@docusaurus/types'; | ||
|
||
import siteConfig from '@generated/docusaurus.config'; | ||
import globalData from '@generated/globalData'; | ||
import i18n from '@generated/i18n'; | ||
import codeTranslations from '@generated/codeTranslations'; | ||
import siteMetadata from '@generated/site-metadata'; | ||
|
||
// Static value on purpose: don't make it dynamic! | ||
// Using context is still useful for testability reasons. | ||
const contextValue: DocusaurusContext = { | ||
siteConfig, | ||
siteMetadata, | ||
globalData, | ||
i18n, | ||
codeTranslations, | ||
}; | ||
|
||
export const Context = React.createContext<DocusaurusContext>(contextValue); | ||
|
||
export function DocusaurusContextProvider({ | ||
children, | ||
}: { | ||
children: ReactNode; | ||
}): JSX.Element { | ||
return <Context.Provider value={contextValue}>{children}</Context.Provider>; | ||
} |
Oops, something went wrong.