Keeping CSS next to the component it styles is one of styled-components' main draws: no hunting through a giant stylesheet, and no guilt about adding to it. But that per-component organization has a cost. When every component gets its own declarations, the same few lines show up over and over — two of three lines matching two of three lines elsewhere. Layout primitives like flexbox make the duplication especially obvious.
The problem, in flexbox terms
Rows and columns are the usual repeated patterns. A row sets flex-direction: row across the children; a column does the same with column:
display: flex;
flex-direction: row; /* the default; in react native, column is the default */
display: flex;
flex-direction: column;
Both can be refined with further properties — justify-content: space-evenly for even spacing across the available width, or align-items to tune alignment. That's fine until several components need the same flex layout with their own small adjustments. Writing three full sets of styles produces a row layout with different gaps each time:
// component one
const ComponentOne = styled.div`
display: flex;
flex-direction: row;
justify-content: flex-start;
`
// component two
const ComponentTwo = styled.div`
display: flex;
flex-direction: row;
justify-content: space-between;
`
// component three
const ComponentThree = styled.div`
display: flex;
flex-direction: row;
justify-content: space-evenly;
`
It works, but the shared lines are now duplicated three times and CSS bloat grows with every new component.
Extending a base component
The cleaner route is to extend a base component and add only the extra styles on top:
// flex row component
const ExampleFlex = `
display: flex;
flex-direction: row;
`
// component one
const ComponentOne = styled(ExampleFlex)`
justify-content: flex-start;
`
// component two
const ComponentTwo = styled(ExampleFlex)`
justify-content: space-between;
`
// component three
const ComponentThree = styled(ExampleFlex)`
justify-content: space-evenly;
`
Beyond cutting repetition, this keeps everything about row direction in one place — switching to a column becomes a one-spot edit instead of three. There is an ordering rule, though: a component that extends another must be declared after its base. Put <ComponentOne /> above <ExampleFlex /> and you get Uncaught ReferenceError: Cannot access 'ExampleFlex' before initialization.
A page-level case: navigation and footer
The pattern scales beyond simple wrappers. Consider a top navigation and a page footer: both need a row on wider screens that collapses to a column on mobile, but the nav aligns left (leaving room for the logo on the right) while the footer links align right. Creating separate <Navigation /> and <Footer /> components makes sense. The nav styles:
const Navigation = styled.div`
align-items: center;
display: flex;
flex-direction: row;
justify-content: space-between;
padding: 10px;
width: calc(100% - 20px);
@media (max-width: 768px) {
flex-direction: column;
}
`
The footer styles:
const Footer = styled.div`
align-items: center;
display: flex;
flex-direction: row;
justify-content: flex-end;
padding: 10px;
width: calc(100% - 20px);
@media (max-width: 768px) {
flex-direction: column;
}
`
The only difference is justify-content. The <LeftSideNav /> component also reuses display: flex with the same media query, and the link components — <NavItem /> and <FooterNavItem /> — are near-identical with slight variations.
The fix is the same one applied above: pull the CSS shared across components into its own component and extend from it, overriding only what each specific case needs. In the example, this trims the JavaScript file holding the styled-components by 10 lines over the previous version. Small on its own, but as an application grows, that habit limits the CSS it ships.
The as polymorphic prop
Extending isn't the only tool. styled-components' as prop applies a component's styles to a specified element instead. It fits interfaces that look the same but sit on different HTML with different semantics — a <button> that submits a form or reworks the current page versus an <a> that navigates to a resource.
Styled identically, two such controls are visually indistinguishable: one may be a real <button>, the other an <a> dressed as one. Instead of duplicating the CSS, style the element as a <Button /> and keep the component alongside the button's JavaScript, then apply the same styles to a <Link /> component by passing a value to the prop:
<Button as="a" href="#" ... >I am a Link!</Button>
The result: no duplicated button CSS, and the freedom to swap in a different HTML tag while retaining the <Button /> styles already defined.
Bottom line
Extending baseline styles — and potentially collecting them in a single globalStyles.js file — makes styled-components code noticeably drier and more manageable. A minimal CSS payload helps site performance and spares future developers the hunt through long stylesheets.



