forked from reactjs/react.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathErrorDecoderContext.tsx
More file actions
23 lines (17 loc) · 829 Bytes
/
ErrorDecoderContext.tsx
File metadata and controls
23 lines (17 loc) · 829 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Error Decoder requires reading pregenerated error message from getStaticProps,
// but MDX component doesn't support props. So we use React Context to populate
// the value without prop-drilling.
// TODO: Replace with React.cache + React.use when migrating to Next.js App Router
import {createContext, useContext} from 'react';
const notInErrorDecoderContext = Symbol('not in error decoder context');
export const ErrorDecoderContext = createContext<
| {errorMessage: string | null; errorCode: string | null}
| typeof notInErrorDecoderContext
>(notInErrorDecoderContext);
export const useErrorDecoderParams = () => {
const params = useContext(ErrorDecoderContext);
if (params === notInErrorDecoderContext) {
throw new Error('useErrorDecoder must be used in error decoder pages only');
}
return params;
};