Skip to main content
Checks whether a given key has been set in the context of a parent component. Must be called during component initialisation.

Signature

Parameters

key
any
required
The key to check for in parent component contexts.

Returns

hasContext
boolean
Returns true if a parent component has set a context with this key, false otherwise.

Example

Conditional Context Usage

Parent Component With Context

Parent Component Without Context

Optional Context Pattern

Library Context Pattern

Notes

  • Must be called during component initialisation (not in callbacks or effects)
  • Use this to provide fallback values when context may not be available
  • Useful for creating reusable components that work both with and without context
  • Helps avoid errors when accessing context that might not exist