UNPKG

@docusaurus/core

Version:

Easy to Maintain Open Source Documentation Websites

23 lines (22 loc) 1.01 kB
/** * 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, { 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(false); export function BrowserContextProvider({ children, }) { const [isBrowser, setIsBrowser] = useState(false); useEffect(() => { setIsBrowser(true); }, []); return <Context.Provider value={isBrowser}>{children}</Context.Provider>; }