Skip to content

Commit

Permalink
Add useRequiredContext
Browse files Browse the repository at this point in the history
  • Loading branch information
garryxiao committed Jan 9, 2025
1 parent 6d0fddb commit fcfe1d9
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@etsoo/react",
"version": "1.8.20",
"version": "1.8.21",
"description": "TypeScript ReactJs UI Independent Framework",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export * from "./uses/useDelayedExecutor";
export * from "./uses/useDimensions";
export * from "./uses/useParamsEx";
export * from "./uses/useRefs";
export * from "./uses/useRequiredContext";
export * from "./uses/useSearchParamsEx";
export * from "./uses/useTimeout";
export * from "./uses/useWindowScroll";
Expand Down
16 changes: 16 additions & 0 deletions src/uses/useRequiredContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from "react";

/**
* Required context
* @param context Context
* @returns Value
*/
export function useRequiredContext<T>(context: React.Context<T>) {
const value = React.useContext(context);

if (value == null) {
throw new Error(`useRequiredContext: ${context.displayName} is required`);
}

return value;
}

0 comments on commit fcfe1d9

Please sign in to comment.