# 错误：'MyContext' 的上下文值未定义。您是否忘记使用带有值的 Provider 包装组件？

- **ID:** `react/context-value-undefined-reducer`
- **领域:** react
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

组件尝试使用未定义的上下文值，因为对应的 Provider 缺失或未提供 value 属性。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| React 18.2.0 | active | — | — |
| React 19.0.0-beta.1 | active | — | — |

## 解决方案

1. ```
   Ensure the component is wrapped in the correct Provider with a value prop. Example: <MyContext.Provider value={someValue}><ChildComponent /></MyContext.Provider>
   ```
2. ```
   Check the context creation file to ensure the context is exported and imported correctly. Use named exports to avoid confusion.
   ```
3. ```
   If using useReducer, pass the dispatch as part of the context value: const [state, dispatch] = useReducer(reducer, initialState); <MyContext.Provider value={{ state, dispatch }}>...</MyContext.Provider>
   ```

## 无效尝试

- **** — The default value is used only if there is no Provider; if a Provider exists without a value, it still passes undefined. (70% 失败率)
- **** — This causes a mismatch between the context used in the Provider and the consumer. (80% 失败率)
- **** — Without a value prop, the Provider passes undefined, which is still undefined. (90% 失败率)
