IconContext allows you to set default properties for all icons within your application or a specific component tree using React’s Context API.
Overview
Instead of passing the same props to every icon component, you can useIconContext.Provider to set defaults that apply to all icons within its scope.
Basic Usage
IconContext Interface
TheIconContext accepts the following properties:
Property Reference
The default size of
1em means icons scale with the font size of their parent element.Configuration Examples
Global Icon Styling
Set consistent styling across your entire application:Theme-Based Icons
- Light Theme
- Dark Theme
Using className for Global Styling
CombineclassName with CSS for consistent icon styling:
Using style for Inline Styling
Apply inline styles to all icons:Nested Contexts
You can nest multipleIconContext.Provider components. Inner contexts override outer ones:
Override Context Props
Props passed directly to icon components override context values:Priority Order
When multiple sources define the same property, React Icons applies them in this order (highest priority first):- Direct props - Props passed directly to the icon component
- Nested context - The closest
IconContext.Providerin the component tree - Parent context - Outer
IconContext.Providercomponents - Default values - Library defaults (size:
'1em', color:currentColor)
Real-World Examples
Navigation Menu
Button Components
Card Components
Advanced: Using attr for SVG Attributes
Theattr property allows you to pass custom SVG attributes to all icons:
TypeScript Support
TheIconContext is fully typed:
See the TypeScript guide for complete type definitions and advanced TypeScript usage.
Best Practices
Use Context for Consistent Styling
Scope Contexts Appropriately
Use className for CSS-Based Styling
Requirements
Next Steps
Icon Imports
Learn about importing icons from different packs
TypeScript
Explore TypeScript types and interfaces