# 类型错误：无法读取 undefined 的属性（读取 'dispatch'）。此错误位于：ConsumerComponent (at App.js:10)

- **ID:** `react/context-not-provided`
- **领域:** react
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 95%

## 根因

组件尝试使用 useContext 访问上下文值，但它没有被相应的 Provider 组件包裹，因此上下文值为 undefined。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| react@18.2.0 | active | — | — |
| react-dom@18.2.0 | active | — | — |

## 解决方案

1. ```
   Wrap the component tree with the Provider: `<MyContext.Provider value={{ dispatch }}><ConsumerComponent /></MyContext.Provider>`
   ```
2. ```
   If the Provider is already present but value is undefined, ensure the value prop is correctly passed: `<MyContext.Provider value={dispatch}>` (not just `value={}`)
   ```

## 无效尝试

- **Importing the context directly and trying to use it as a value** — The context object itself is not the value; it must be provided via a Provider. (75% 失败率)
- **Adding a default value to createContext but not wrapping in Provider** — The default value is only used when there is no Provider at all, but if the component is wrapped in a Provider without a value prop, the context is still undefined. (50% 失败率)
- **Using useContext inside a class component** — useContext is a hook and cannot be used in class components; use Context.Consumer instead. (65% 失败率)
