Maintainable React
Maintainable React
Test-Driven Development, Refactoring and Clean Code.
About the Book
Write Clean Code in React—Without Guesswork
Modern frontend development is more complex than ever. React gives you power, but with that comes decisions: How should you structure components? What makes code "clean"? When is abstraction helpful—and when is it harmful?
This book is your practical guide to writing maintainable, readable, and flexible React code by applying proven design principles to real-world examples.
Instead of abstract theory, you'll follow step-by-step refactoring journeys—from messy, hard-to-change codebases to well-structured, thoughtful designs. Along the way, you'll learn how to spot code smells, apply small and effective refactorings, and avoid common anti-patterns.
Whether you're a developer looking to sharpen your craft or preparing for a system design interview, this book gives you a toolkit for thinking clearly about code.
Translations
Table of Contents
-
Preface
- Do we need another book on this topic?
- Who is this book for?
- How the book is structured
- References
-
Chapter 0 — React Clean Code: Embracing Elegance in Code
- Introduction: Why Clean Code Matters in React
- What Does “Clean Code” Really Mean?
- Symptom 1: The Long File
- Symptom 2: Hard to Test
- Symptom 3: Hard to Change
- Symptom 4: Ignoring Fundamental Principles
- What Clean Code Looks Like
- React-Specific Cleanup Techniques
- Summary: The First Step Toward Clean React Code
- References
-
Chapter 1/2 – Navigating the Growth of Your React Application
- Frontend Projects Are More Complex Than They Look
- How to Split Your Project
- Summary and Key Takeaways
-
Chapter 1 – A Brief Introduction to Test-Driven Development
- What Is Test-Driven Development?
- Where Beginners Get Stuck
- Writing the First Test
- Adding One More Test
- Summary
-
Chapter 2 – Todo Application: Building Core Features with TDD
- Starting the Todo Application
- Rendering a Single Todo Item
- Rendering Multiple Todo Items
- Using a Better Data Structure
- Adding a New Todo Item
- Improvements and Final Version
- A Note on File Organization
- Summary
-
Chapter 3 – Refactor: Separation of Concerns
- What Is Separation of Concerns?
- Why Separation of Concerns Matters
- Step 1: Extract a Component
- Step 2: Update the Todo Component
- Why This Refactoring Works
- Summary
-
Chapter 4 – Adding a New Feature: Mark Item As Done
- Write the Test First
- Tasking the Feature
- Step 1: Update the Type
- Step 2: Handle the Click
- Step 3: Filter Completed Items
- Recap: What We Just Achieved
- Summary
-
Chapter 5 – Refactor: Split Logic Into a Custom Hook
- Why Refactor Again?
- Step 1: Rename the Component
- Step 2: Extract a List Component
-
Step 3: Simplify
TodoInput
and Moveuuid
- Step 4: Introduce a Custom Hook
- Why This Matters
- What We’ve Learned
- Summary
- Wrapping Up
-
Chapter 6 – Composition Foundation
- The Children / Slots Pattern
- Why Not Just Use Props?
- Adding More Slots
- What We’ve Learned
- Summary
-
Chapter 7 - Refactor - AddressContainer
- Why Render Props?
- Using Render Prop to Decouple Logic and View
- Rendering With Custom Logic
- Summary
- What’s Next
-
Chapter 8 - Composition Example 1: The Avatar Component
- The Tooltip Prop
- Breaking the Dependency Chain
- Summary
-
Chapter 9 - Composition Example 2 - InlineEdit
- The InlineEdit component
- The validate prop
-
Option 1: Add an
invalidView
prop -
Option 2: Extend
editView
with error state - Summary
-
Chapter 10 - The Refactor of StackView Component
- The StackView Component
- Tests
- Optional Header and Footer Component
- New Requirement - the User Profile
- Break It Down and Reassemble It Back
- Summary
-
Chapter 11 – Business Logic Leaking
- Where Should Business Logic Go?
- React is The View
- What is Business Logic?
- Signs of Logic Leakage
- A Word About Habits
- Summary
-
Chapter 12 – Fix the Leakage: Data Modeling
- The Address Input Example
- Current Implementation
- Refactor Step 1: Separation of Concerns
- Refactor Step 2: Custom Hook
- Step 3: Model the Data
- Why It Matters
- Summary
-
Chapter 13 - Project Simulation: Direct To Boot
- Start with a Test
- A Disabled Button by Default
- Avoiding Fragile Mocks
- Enable Button When Order is Ready
- Polling Until Ready
- Refactor with Custom Hook
- Summary
-
Chapter 14 – Feature: Notify the Store
- Refactor: Treat Test Code as First-Class Code
- Clean Up the Component Logic
- Extracting a Custom Hook
- Summary
-
Chapter 15 - Error Handling
- Why This Needs a New State
- Common Mistake: Overloading Booleans
- Updating the User Message
- Refactoring: Breaking Up Conditional UI
- Section Abstraction
- Summary
-
Chapter 16 - What You’ve Learned
- What’s Next?
- What You’ve Learned
- Stay Connected
-
Appendix 10 Refactorings to Boost your Clean Code Efficiency
- The most common 10 refactorings
- Code smells
- Common Refactorings
- The Problem - ROT13
- The implementation
- Summary
The Leanpub 60 Day 100% Happiness Guarantee
Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.
Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.
You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!
So, there's no reason not to click the Add to Cart button, is there?
See full terms...
Earn $8 on a $10 Purchase, and $16 on a $20 Purchase
We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.
(Yes, some authors have already earned much more than that on Leanpub.)
In fact, authors have earnedover $14 millionwriting, publishing and selling on Leanpub.
Learn more about writing on Leanpub
Free Updates. DRM Free.
If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).
Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.
Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.
Learn more about Leanpub's ebook formats and where to read them