From 9f6e6b39cf1ae32cd0be6fd31f94413a4e40976b Mon Sep 17 00:00:00 2001 From: Thomas Beer <71586988+Tommypop2@users.noreply.github.com> Date: Tue, 8 Apr 2025 14:30:57 +0100 Subject: [PATCH 1/2] dev: add warning when context is null --- packages/solid/src/reactive/signal.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/solid/src/reactive/signal.ts b/packages/solid/src/reactive/signal.ts index 3e6aa1e8..938a1f7c 100644 --- a/packages/solid/src/reactive/signal.ts +++ b/packages/solid/src/reactive/signal.ts @@ -1201,9 +1201,11 @@ export function createContext( */ export function useContext(context: Context): T { let value: undefined | T; - return Owner && Owner.context && (value = Owner.context[context.id]) !== undefined - ? value - : context.defaultValue; + let ctx = Owner && Owner.context && (value = Owner.context[context.id]) !== undefined + ? value + : context.defaultValue; + IS_DEV && !ctx && console.warn("`useContext` returns null. Is it being called inside a provider?") + return ctx; } export type ResolvedJSXElement = Exclude; From bc6c19b6d64aa0b45a5a6dbbe1abd7b9bcf0dcf4 Mon Sep 17 00:00:00 2001 From: Thomas Beer <71586988+Tommypop2@users.noreply.github.com> Date: Tue, 8 Apr 2025 14:44:22 +0100 Subject: [PATCH 2/2] dev: update context warning --- packages/solid/src/reactive/signal.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/solid/src/reactive/signal.ts b/packages/solid/src/reactive/signal.ts index 938a1f7c..064cca90 100644 --- a/packages/solid/src/reactive/signal.ts +++ b/packages/solid/src/reactive/signal.ts @@ -1204,7 +1204,7 @@ export function useContext(context: Context): T { let ctx = Owner && Owner.context && (value = Owner.context[context.id]) !== undefined ? value : context.defaultValue; - IS_DEV && !ctx && console.warn("`useContext` returns null. Is it being called inside a provider?") + IS_DEV && !ctx && console.warn("`useContext` is returning undefined. Is it being called inside a provider?") return ctx; }