Skip to content

[2.0] createContext(undefined) throws ContextNotFoundError, even when a provider passes undefined #4181

Closed

Description

@devagrawal09

Describe the bug

getContext (packages/signals/src/core/context.ts) treats undefined as "not found":

let value = owner._context[context.id];
if (value === undefined) value = context.defaultValue;
if (value === undefined) throw new ContextNotFoundError();

So createContext<T | undefined>(undefined), the most common optional-context idiom in React and Solid 1 (where useContext returned undefined), throws when read outside a provider. It also throws when a provider explicitly passes undefined. Inside a computation, the throw halts reactivity for the whole app (#4178).

The message, "Context must either be created with a default value or a value must be provided before accessing it", sounds wrong when the caller did pass a default.

Your Example Website or App

Minimal repro inline below (signals-only).

Steps to Reproduce the Bug or Issue

import { createContext, getContext, setContext, createRoot, createSignal, createRenderEffect, flush } from "@solidjs/signals";

const Opt = createContext<string | undefined>(undefined);
const OptNull = createContext<string | null>(null);

createRoot(() => getContext(Opt));                              // throws ContextNotFoundError
createRoot(() => { setContext(Opt, "x"); getContext(Opt); });   // "x"
createRoot(() => { setContext(Opt, undefined); getContext(Opt); }); // throws ContextNotFoundError
createRoot(() => getContext(OptNull));                          // null

// Inside a computation:
const [show, setShow] = createSignal(false);
const [b, setB] = createSignal(0);
createRoot(() => createRenderEffect(b, v => console.log("b", v)));
createRoot(() => createRenderEffect(() => (show() ? getContext(Opt) : null), () => {}));
flush();
setShow(true); flush(); // StatusError (ContextNotFoundError) + [REACTIVITY_HALTED]
setB(1); flush();       // "b 1" never logs

Expected behavior

  • Distinguish "no default argument" from an explicit undefined default (arguments.length or a sentinel), and throw only for the former.
  • Treat a provider's explicit value={undefined} as a provided value.
  • If that's not wanted, at minimum say in the error message and the docs/cheatsheet that undefined counts as missing, and that null is the way to make a context optional.

Screenshots or Videos

No response

Platform

  • OS: macOS (Darwin 25.5)
  • Browser: n/a (reproduced in Node v24.18.0 with vitest, importing source)
  • Version: solid next @ b0ad1df (2026-10-20, after 2.0.0-rc.16)

Additional context

This came up while verifying examples for the From React docs additions (solidjs/solid-docs#1664). It's also a silent migration break from 1.x, where useContext returned undefined.

— Claude, on behalf of @devagrawal09

Activity

  1. changed the title [-][2.0] throws ContextNotFoundError, even when a provider passes [/-] [+][2.0] `createContext(undefined)` throws ContextNotFoundError, even when a provider passes `undefined`[/+] on Oct 21, 2026
  2. ryansolid commented on Oct 21, 2026

    @ryansolid
    Member

    Fixed in #4201, merged to next as 690228a. It will ship in the next RC.

    — Claude via Claude Code


    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions