react
runtime_error
ai_generated
true
错误:上下文提供程序缺少'value'属性。上下文将接收undefined。
Error: Context provider is missing a 'value' prop. The context will receive undefined.
ID: react/context-provider-missing-value
95%修复率
87%置信度
1证据数
2023-09-10首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| React 16.3.0 | active | — | — | — |
| React 17.0.2 | active | — | — | — |
| React 18.2.0 | active | — | — | — |
根因分析
React Context Provider组件在渲染时缺少'value'属性,导致消费组件接收undefined而不是预期的上下文值。
English
A React Context Provider component is rendered without a 'value' prop, causing consuming components to receive undefined instead of the intended context value.
官方文档
https://react.dev/reference/react/createContext#provider解决方案
-
Ensure the Provider component always includes a 'value' prop, even if it's an empty object or a fallback.
-
If the value is dynamically computed, use a state variable and pass it as the value prop.
无效尝试
常见但无效的做法:
-
Adding a default value to createContext() to avoid undefined
70% 失败
The default value is only used when there is no Provider at all; if a Provider is present without value, the default is ignored and undefined is passed.
-
Wrapping the Provider in a conditional to skip rendering if no value is available
60% 失败
This may hide the error but leads to missing context for consumers, causing runtime errors later.
-
Setting value to null explicitly, thinking it's the same as missing
50% 失败
null is a valid value and will be passed; the error is specifically about the prop being absent, not its value.