Using React-Bootstrap's `as` Prop: Swap Elements Without Losing Bootstrap Styling
React-Bootstrap's `as` prop lets you swap rendered elements while preserving Bootstrap styling. Learn how to use it correctly with React Router, avoid common pitfalls, and verify your implementation.
04 Dec 2025, 07:19 UTC

The Problem: Semantic HTML vs Styled Components
When building React applications with React-Bootstrap, you often need components that look like Bootstrap elements but render as different HTML tags. A button that should behave like a navigation link is a common scenario. Using a <button> for navigation breaks browser features like middle-click to open in a new tab, while an <a> without proper styling loses Bootstrap's button appearance.
The Solution: The `as` Prop
React-Bootstrap solves this with the as prop, which swaps the rendered element while preserving Bootstrap classes, variants, and internal wiring. The component renders the specified element type as the root, then forwards className, event handlers, and relevant props.
Basic Configuration Example
Here's how to render a button that looks like a Bootstrap button but functions as a navigation link:
import { Button } from 'react-bootstrap';
import { Link } from 'react-router-dom';
// Renders as an anchor tag with button styling
<Button as={Link} to="/reports" variant="primary">
View Reports
</Button>
// Alternative with plain HTML anchor
<Button as="a" variant="primary" href="/reports" target="_blank" rel="noopener">
Open Reports
</Button>
How It Works
The as prop accepts any valid React element type—a string tag like 'a' or 'div', or a component like React Router's Link. React-Bootstrap renders this as the root element and applies its internal logic on top. Since Bootstrap's .btn class is element-agnostic, an anchor with .btn and .btn-primary maintains button styling while keeping proper link semantics.
Common Use Cases
- Navigation Links:
<Button as={Link} to="/path">for SPA navigation - External Links:
<Button as="a" href="url" target="_blank">for downloads - Semantic Markup:
<Card as="article">for proper document structure - List Items:
<ListGroup.Item as="li">for correct nesting
Important Limitations
The as prop only changes the root element—it does not modify styling scope or component behavior. Props must be valid for the target element. For example, Nav.Link and Breadcrumb.Item render anchors by default and expect href and active props.
TypeScript Considerations
React-Bootstrap's TypeScript types for as are permissive (ElementType), so invalid prop combinations may compile but fail at runtime. Always verify that props make sense for the element you're rendering to.
Common Mistakes to Avoid
- Ref Forwarding: Custom components passed to
asmust forward refs viaReact.forwardRef. Overlay-based components likeTooltip,Popover, andDropdownwill fail to position correctly without proper ref forwarding. - Keyboard Semantics: Using
as="div"withonClickon interactive components drops keyboard activation. Addrole="button",tabIndex="0", and handleonKeyDownfor Enter/Space. - Disabled State on Anchors: The
disabledprop on an anchor element only applies styling andaria-disabled—there's no native disabled attribute. Handle click events to prevent navigation when disabled.
Version Compatibility
The as prop behaves consistently across React-Bootstrap v1 (Bootstrap 4) and v2 (Bootstrap 5), but underlying class names differ. For example, v1's custom-control and form-row don't exist in v2. Upgrading may change the styling produced by as-rendered markup even though the prop's mechanics remain the same.
Verification Checklist
- Inspect the DOM in browser devtools—the root node should be the
astarget with expected Bootstrap classes - Check the official React-Bootstrap documentation for the
asprop entry and its stated type - Test keyboard activation, focus order, and screen-reader output for non-standard elements
- Confirm your React-Bootstrap major version (v1 vs v2) and Bootstrap CSS version in use
Best Practices Summary
- Use
asfor semantic correctness, not visual customization—prefervariant,size, orbsPrefixfor styling - Always verify props are valid for the target element type
- Ensure custom components forward refs properly
- Test interactive behavior including keyboard navigation
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.