mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-04 20:57:17 +02:00
feat(v2): Error when hooks depends on context is used outside of Layout (#2974)
This commit is contained in:
parent
2b4b6f73b7
commit
11b7ce529c
4 changed files with 16 additions and 16 deletions
|
@ -7,10 +7,6 @@
|
||||||
|
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
|
||||||
const ThemeContext = React.createContext({
|
const ThemeContext = React.createContext();
|
||||||
isDarkTheme: false,
|
|
||||||
setLightTheme: () => {},
|
|
||||||
setDarkTheme: () => {},
|
|
||||||
});
|
|
||||||
|
|
||||||
export default ThemeContext;
|
export default ThemeContext;
|
||||||
|
|
|
@ -7,14 +7,6 @@
|
||||||
|
|
||||||
import {createContext} from 'react';
|
import {createContext} from 'react';
|
||||||
|
|
||||||
const UserPreferencesContext = createContext({
|
const UserPreferencesContext = createContext();
|
||||||
// Tab group choice.
|
|
||||||
tabGroupChoices: {},
|
|
||||||
setTabGroupChoices: () => {},
|
|
||||||
|
|
||||||
// Announcement bar.
|
|
||||||
isAnnouncementBarClosed: false,
|
|
||||||
closeAnnouncementBar: () => {},
|
|
||||||
});
|
|
||||||
|
|
||||||
export default UserPreferencesContext;
|
export default UserPreferencesContext;
|
||||||
|
|
|
@ -10,7 +10,13 @@ import {useContext} from 'react';
|
||||||
import ThemeContext from '@theme/ThemeContext';
|
import ThemeContext from '@theme/ThemeContext';
|
||||||
|
|
||||||
function useThemeContext() {
|
function useThemeContext() {
|
||||||
return useContext(ThemeContext);
|
const context = useContext(ThemeContext);
|
||||||
|
if (context == null) {
|
||||||
|
throw new Error(
|
||||||
|
'`useThemeContext` is used outside of `Layout` Component. See https://v2.docusaurus.io/docs/theme-classic#usethemecontext.',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return context;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default useThemeContext;
|
export default useThemeContext;
|
||||||
|
|
|
@ -10,7 +10,13 @@ import {useContext} from 'react';
|
||||||
import UserPreferencesContext from '@theme/UserPreferencesContext';
|
import UserPreferencesContext from '@theme/UserPreferencesContext';
|
||||||
|
|
||||||
function useUserPreferencesContext() {
|
function useUserPreferencesContext() {
|
||||||
return useContext(UserPreferencesContext);
|
const context = useContext(UserPreferencesContext);
|
||||||
|
if (context == null) {
|
||||||
|
throw new Error(
|
||||||
|
'`useUserPreferencesContext` is used outside of `Layout` Component.',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return context;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default useUserPreferencesContext;
|
export default useUserPreferencesContext;
|
||||||
|
|
Loading…
Add table
Reference in a new issue