mirror of
https://github.com/lukevella/rallly.git
synced 2025-04-29 18:26:34 +02:00
14 lines
360 B
TypeScript
14 lines
360 B
TypeScript
import React from "react";
|
|
|
|
export const useRequiredContext = <T extends any>(
|
|
context: React.Context<T | null>,
|
|
errorMessage?: string,
|
|
) => {
|
|
const contextValue = React.useContext(context);
|
|
if (contextValue === null) {
|
|
throw new Error(
|
|
errorMessage ?? `Missing context provider: ${context.displayName}`,
|
|
);
|
|
}
|
|
return contextValue;
|
|
};
|