mirror of
https://github.com/reactjs/react.dev.git
synced 2026-02-26 18:58:17 +00:00
Feat: error-decoder (#6214)
* Feat: port error-decoder * Fix: do not choke on empty invariant * Refactor: read url query from `useRouter` * Fix: argsList can contains `undefined` * Fix: handle empty string arg * Feat: move error decoder to the separate page * Fix: wrap error decoder header in <Intro /> * Perf: cache GitHub RAW requests * Refactor: apply code review suggestions * Fix: build error * Refactor: apply code review suggestions * Discard changes to src/content/index.md * Fix: animation duration/delay * Refactor: read error page from markdown * Fix lint * Fix /error being 404 * Prevent `_default.md` being included in `[[...markdownPath]].md` * Fix custom error markdown reading * Updates --------- Co-authored-by: Ricky Hanlon <rickhanlonii@gmail.com>
This commit is contained in:
23
src/components/ErrorDecoderContext.tsx
Normal file
23
src/components/ErrorDecoderContext.tsx
Normal file
@@ -0,0 +1,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;
|
||||
};
|
||||
Reference in New Issue
Block a user