Skip to content

dev: add warning when context is undefined #2466

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/solid/src/reactive/signal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1201,9 +1201,11 @@ export function createContext<T>(
*/
export function useContext<T>(context: Context<T>): 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` is returning undefined. Is it being called inside a provider?")
return ctx;
}

export type ResolvedJSXElement = Exclude<JSX.Element, JSX.ArrayElement>;
Expand Down