Atomic Design In React: A Practical Guide

Zeeshan Ahmed
3 min readMar 30, 2023

--

Developing complex user interfaces (UI) in React can be a daunting task, especially when you want a maintainable and scalable architecture that doesn’t drive you nuts. In times like these, Atomic Design comes to the rescue like a superhero with a cape made of React components. Thanks to Brad Frost, this methodology helps us break down UI components into smaller, reusable parts, and then piece them back together into a beautiful Frankenstein’s monster of a UI.

Here’s a link to the post by the man himself https://bradfrost.com/blog/post/atomic-web-design/

Hold on tight as we embark on this atomic adventure to create a blog application with React, following the five levels of Atomic Design principles. Spoiler alert: It’s going to be a blast!

Atoms: The tiniest UI quirks

Atoms are the fundamental building blocks of UI, like tiny buttons, inputs, labels, or icons that form the basis of more complex components. In the React universe, atoms are like small planets that revolve around their own functional components, each with its own unique UI superpower.

For our blog application, let’s create a Button and TextInput atom that is as small and powerful as Ant-Man:

Button.js

function Button({ onClick, children }) {
return <button onClick={onClick}>{children}</button>;
}

export default Button;

TextInput.js

function TextInput({ placeholder, value, onChange }) {
return (
<input type="text" placeholder={placeholder} value={value} onChange={onChange} />
);
}

export default TextInput;

Molecules: When atoms party together

Molecules are like atoms that decided to join forces and create a supergroup, working together to make a specific piece of functionality. In React, molecules are the outcome of atoms joining hands (or components) to create a functional ensemble. For our blog application, we’ll unite the Button and TextInput atoms into a dynamic duo called the SearchBar molecule:

SearchBar.js

import Button from './atoms/Button';
import TextInput from './atoms/TextInput';

function SearchBar({ placeholder, value, onChange, onSubmit }) {
return (
<div>
<TextInput
placeholder={placeholder}
value={value}
onChange={onChange}
/>
<Button onClick={onSubmit}>Search</Button>
</div>
);
}

export default SearchBar;

Organisms: The bustling UI metropolis

Organisms are bustling hubs of activity, where complex UI components are formed by assembling multiple molecules and/or atoms. These components represent distinct sections of a user interface, like a well-organized cityscape. In our React blog application, we’ll build a Header organism that houses our SearchBar molecule:

Header.js

import SearchBar from './molecules/SearchBar';

function Header({ onSearch }) {
return (
<header>
<h1>Blogtropolis: A City of Articles</h1>
<SearchBar
placeholder="Explore the city of articles"
onSubmit={onSearch}
/>
</header>
);
}

export default Header;

Templates: The blueprint for UI harmony

Templates are like architectural blueprints for UI, defining the layout and structure of a page or screen by strategically placing organisms, molecules, and atoms. In React, templates are like city planners that arrange organisms and other components into a coherent layout. For our blog application, let’s create a MainTemplate:

MainTemplate.js

import Header from './organisms/Header';

function MainTemplate({ children, onSearch }) {
return (
<div>
<Header onSearch={onSearch} />
<main>{children}</main>
</div>
);
}

export default MainTemplate;

Pages: The culmination of our atomic journey

Pages are the ultimate destination of our atomic journey, where templates are populated with real data and content. They represent a complete instance of a user interface, like a city filled with inhabitants. In our React blog application, let’s bring our MainTemplate to life with a HomePage:

HomePage.js

import { useState } from 'react';
import MainTemplate from './templates/MainTemplate';

function HomePage() {
const [searchValue, setSearchValue] = useState('');
const handleSearch = () => {
console.log(`Embarking on a search quest for: ${searchValue}`);
};
return (
<MainTemplate onSearch={handleSearch}>
<h2>Welcome to Blogtropolis: A City of Articles</h2>
<p>Roam the streets and discover articles on a myriad of topics.</p>
</MainTemplate>
);
}

export default HomePage;

Conclusion:

And there you have it — a practical guide to applying Atomic Design principles in React UI. We’ve successfully built a blog application by creating atoms, molecules, organisms, templates, and pages that join forces like a comedic superhero team. This modular approach to UI design will not only make your life easier but also for other developers.

Happy coding! 💻

--

--

Zeeshan Ahmed
0 Followers

A software engineer and an internet technology enthusiast with a bachelor's in computer sceince engineering and on a on a lifelong mission of learning & growth.