feat(v2): Error when hooks depends on context is used outside of Layout (#2974)

This commit is contained in:
Sam Zhou 2020-06-23 09:55:18 -04:00 committed by GitHub
parent 2b4b6f73b7
commit 11b7ce529c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 16 deletions

View file

@ -7,10 +7,6 @@
import React from 'react';
const ThemeContext = React.createContext({
isDarkTheme: false,
setLightTheme: () => {},
setDarkTheme: () => {},
});
const ThemeContext = React.createContext();
export default ThemeContext;

View file

@ -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;

View file

@ -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;

View file

@ -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;