IconBase
TheIconBase component is the foundational component that renders all icons in React Icons. It handles context integration, size computation, styling, and SVG rendering.
Usage
Props
Record<string, string>
Additional SVG attributes to apply to the root SVG element. These are merged with context attributes and other props.
string | number
Icon size. Can be a number (interpreted as pixels) or a string with units (e.g., “24px”, “1.5em”). Defaults to “1em” if not specified in props or context.
string
Icon color. Accepts any valid CSS color value. If not specified, inherits from the IconContext or uses “currentColor”.
string
Accessibility title for the icon. When provided, renders a
<title> element inside the SVG for screen readers.string
CSS class name(s) to apply to the SVG element. Merged with className from IconContext if both are present.
React.CSSProperties
Inline styles to apply to the SVG element. Merged with style from IconContext, with component props taking precedence.
React.ReactNode
Child elements to render inside the SVG. Typically contains
<path>, <circle>, or other SVG elements.React.SVGAttributes<SVGElement> are also supported and passed through to the underlying <svg> element.
Implementation Details
The IconBase component:- Consumes the
IconContextto apply global icon configuration - Merges context values with component props (props take precedence)
- Sets default SVG attributes:
stroke="currentColor",fill="currentColor",strokeWidth="0" - Computes final size from props, context, or defaults to “1em”
- Combines className values from context and props
- Merges style objects with proper precedence (context < props)
- Renders an optional
<title>element for accessibility
GenIcon
A factory function that generates icon components from icon tree data structures. This is the primary method used internally to create all icon components in React Icons.Signature
Parameters
IconTree
required
The icon tree structure containing the SVG element definitions.
Returns
Returns a React functional component that acceptsIconBaseProps and renders the icon.
Usage
How It Works
- Takes an
IconTreedata structure describing the SVG elements - Returns a function component that accepts
IconBaseProps - The returned component renders an
IconBasewith the tree structure converted to React elements - Recursively processes nested child elements to build the complete SVG structure
Example: Creating a Custom Icon Pack
Source Reference
For implementation details, see:iconBase.tsx:39-81- IconBase component implementationiconBase.tsx:23-29- GenIcon factory functioniconBase.tsx:11-22- Tree2Element helper for recursive rendering