A simple and reusable component for displaying an icon with a badge using Styled Components in React. The badge is ideal for showing counts, notifications, or other small bits of information.
styled-components
for dynamic styling.styled-components
in your project.Install styled-components
if not already available:
npm install styled-components
Use the provided styled components to build your IconWithBadge
:
import {
IconWithBadgeContainer,
BadgeContainer,
BadgeText,
} from "./IconWithBadgeSample";
const IconWithBadge = () => (
<IconWithBadgeContainer>
<img src="icon.png" alt="icon" style={{ width: 24, height: 24 }} />
<BadgeContainer>
<BadgeText>5</BadgeText>
</BadgeContainer>
</IconWithBadgeContainer>
);
export default IconWithBadge;
IconWithBadge
component in your app:import React from "react";
import IconWithBadge from "./IconWithBadge";
function App() {
return (
<div>
<h1>Welcome to Icon With Badge Example</h1>
<IconWithBadge />
</div>
);
}
export default App;
The styling is defined using Styled Components. Below is a quick overview of each styled component:
IconWithBadgeContainer
:BadgeContainer
:BadgeText
: