Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jun 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions packages/docusaurus-theme-classic/src/theme/ThemeContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@

import React from 'react';

const ThemeContext = React.createContext({
isDarkTheme: false,
setLightTheme: () => {},
setDarkTheme: () => {},
});
const ThemeContext = React.createContext();

export default ThemeContext;
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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;