From ec98f07352ca9e1eed08f681b7de253f7c9f425d Mon Sep 17 00:00:00 2001 From: Richard Nolan Date: Thu, 7 Jul 2022 12:37:15 +0100 Subject: [PATCH 1/2] Export useChartContext --- src/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/index.ts b/src/index.ts index dc1428d0..80f10426 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,3 @@ export { Chart } from './components/Chart' export * from './types' +export { default as useChartContext } from './utils/chartContext' From 6568edef529f806cd3b255e954a248641b931d68 Mon Sep 17 00:00:00 2001 From: Richard Nolan Date: Thu, 7 Jul 2022 12:37:47 +0100 Subject: [PATCH 2/2] Check within provider and force types --- src/utils/chartContext.tsx | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/utils/chartContext.tsx b/src/utils/chartContext.tsx index 16f3bee7..2fb24103 100644 --- a/src/utils/chartContext.tsx +++ b/src/utils/chartContext.tsx @@ -15,5 +15,8 @@ export function ChartContextProvider({ } export default function useChartContext() { - return React.useContext(chartContext)() as ChartContextValue + const ctx = React.useContext(chartContext)() as ChartContextValue + if (!ctx) + throw new Error('useChartContext can only be used within a Chart Provider') + return ctx as ChartContextValue }