react
runtime_error
ai_generated
true
TypeError: Cannot read properties of undefined (reading 'value') when accessing context
ID: react/context-value-undefined-in-provider
94%Fix Rate
87%Confidence
1Evidence
2023-05-18First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| React 18.2.0 | active | — | — | — |
| React 17.0.2 | active | — | — | — |
Root Cause
A component consumes a context using useContext but the corresponding Provider is missing, or the Provider does not supply a 'value' prop, resulting in undefined context value.
generic中文
一个组件使用 useContext 消费 context,但相应的 Provider 缺失,或者 Provider 没有提供 'value' prop,导致 context 值为 undefined。
Official Documentation
https://react.dev/reference/react/useContext#providing-contextWorkarounds
-
95% success Ensure the component consuming the context is wrapped in the corresponding Provider component with a 'value' prop.
Ensure the component consuming the context is wrapped in the corresponding Provider component with a 'value' prop.
-
85% success Add a null/undefined check before accessing properties on the context value to provide a fallback UI or default value.
Add a null/undefined check before accessing properties on the context value to provide a fallback UI or default value.
中文步骤
Ensure the component consuming the context is wrapped in the corresponding Provider component with a 'value' prop.
Add a null/undefined check before accessing properties on the context value to provide a fallback UI or default value.
Dead Ends
Common approaches that don't work:
-
60% fail
If the consumer tries to access nested properties that aren't in the default value, it still throws undefined errors.
-
75% fail
Even if the Provider is present, importing a different context instance means the consumer doesn't see the provided value.