Skip to content

Commit

Permalink
feat(v2): Error when hooks depends on context is used outside of Layout
Browse files Browse the repository at this point in the history
  • Loading branch information
SamChou19815 committed Jun 23, 2020
1 parent 2b4b6f7 commit 289875d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
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;

0 comments on commit 289875d

Please sign in to comment.