mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-04 12:47:14 +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';
|
||||
|
||||
const ThemeContext = React.createContext({
|
||||
isDarkTheme: false,
|
||||
setLightTheme: () => {},
|
||||
setDarkTheme: () => {},
|
||||
});
|
||||
const ThemeContext = React.createContext();
|
||||
|
||||
export default ThemeContext;
|
||||
|
|
|
@ -7,14 +7,6 @@
|
|||
|
||||
import {createContext} from 'react';
|
||||
|
||||
const UserPreferencesContext = createContext({
|
||||
// Tab group choice.
|
||||
tabGroupChoices: {},
|
||||
setTabGroupChoices: () => {},
|
||||
|
||||
// Announcement bar.
|
||||
isAnnouncementBarClosed: false,
|
||||
closeAnnouncementBar: () => {},
|
||||
});
|
||||
const UserPreferencesContext = createContext();
|
||||
|
||||
export default UserPreferencesContext;
|
||||
|
|
|
@ -10,7 +10,13 @@ import {useContext} from 'react';
|
|||
import ThemeContext from '@theme/ThemeContext';
|
||||
|
||||
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;
|
||||
|
|
|
@ -10,7 +10,13 @@ import {useContext} from 'react';
|
|||
import UserPreferencesContext from '@theme/UserPreferencesContext';
|
||||
|
||||
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;
|
||||
|
|
Loading…
Add table
Reference in a new issue