f-react-use-modal
Version:
f-react-use-modal
13 lines (11 loc) • 349 B
text/typescript
import React from 'react';
export const useNonNullableContext = <T>(
context: React.Context<T>,
name?: string
): NonNullable<T> => {
const maybeContext = React.useContext(context);
if (maybeContext === null || maybeContext === undefined) {
throw new Error(`context "${name}" was used without a Provider`);
}
return maybeContext;
};