[2.0] `createContext(undefined)` throws ContextNotFoundError, even when a provider passes `undefined`
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 56/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- javascript, typescript
调研方向
Start in packages/signals/src/core/context.ts and read getContext alongside the reported examples. Reproduce the cases for an explicit undefined default and provider value, then check how context errors affect computations. Done means the chosen behavior matches the expected cases and the error or docs explain the undefined behavior; the issue does not name a test file.
由索引模型根据 Issue 内容生成。
描述
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
undefineddefault (arguments.lengthor 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
undefinedcounts as missing, and thatnullis 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@ b0ad1df50 (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.
- 主要语言
- TypeScript
- 星标
- 36.1k
- 派生
- 1.1k
- 平均合并
- 11 小时 17 分钟
- 30 天内合并 PR
- 371
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
solidjs/solid 的其他 Issue
-
`lazy()`: remounting an already-loaded component renders empty (or the Suspense fallback) since 1.9.14可能已有人在做 @itisrmk 于 4 天前认领。 未关闭1.x
难度 2/5 1-3 小时 新手友好度 62/100
维护者通常 1 天内回复
-
[2.0] The MIGRATION.md replacement for `from()` keeps the upstream subscription after dispose; add a subscription helper可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭
难度 5/5 一周以上 新手友好度 35/100
维护者通常 1 天内回复
-
enhancement
难度 5/5 一周以上 新手友好度 35/100
维护者通常 1 天内回复
-
`createRoot`'s dispose function keeps `fn` alive, so `<For>`/`<Index>` rows retain removed list items可能已有人在做 @boombang 于 6 天前认领。 未关闭1.x
难度 4/5 1-2 天 新手友好度 45/100
维护者通常 1 天内回复
-
signals: a landing reveals a held write its stale reader re-derives onto a new flight (U-5, D-9)未关闭
难度 5/5 一周以上 新手友好度 12/100
维护者通常 1 天内回复
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 68/100
chaitin/MonkeyCode#1437 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 69/100
carbon-design-system/carbon-ai-chat#2615 ·
维护者通常 1 天内回复
-
e2e-failure ready-to-code
难度 2/5 1-3 小时 新手友好度 76/100
redhat-developer/rhdh-plugin-export-overlays#4486 · 1 条评论 ·
维护者通常 1 天内回复
-
[Bug]: fileUploadMatrix.test.ts URL case does a real DNS lookup and fails offline可能已有人在做 @r2jaff 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 72/100
ComposioHQ/composio#5054 ·
维护者通常 1 天内回复