• React
  • Code quality

Clean code in React projects: why is it hard, and how do you do it?

Why React projects fall apart over time, and ten practices that slow the decay.

When a React project begins, everything is spotless. App.jsx is empty, the folder structure is simple, you can count the components on one hand. Then things change: new components, new needs, deadline pressure, “leave it like this for now” notes… and one day you find the project has become a minefield you’re afraid to walk through.

React’s strength, its component model, can turn into chaos where wheat and chaff are mixed: a 500-line utils.js, the same button written in three places, API calls scattered through a useEffect, props drilled five levels deep. The worst part is that the code “works” but no longer feels like yours.

Why is it so hard?

Being a React developer isn’t only building interfaces that work; it means writing with scalability, maintainability and team compatibility in mind. That takes time, patience and discipline. Writing a component quickly is easy. Making it reusable, testable and understandable six months later is the real challenge.

What is clean code?

As Robert C. Martin (Uncle Bob) puts it in Clean Code, clean code isn’t only readable; it’s understandable, testable and extensible. In React that means:

  • Components do one job (single responsibility).
  • The flow of props and state is clear and predictable.
  • Side effects are controlled and isolated.
  • Dependencies are few and visible.

1. Keep components small

If a component has grown long, it’s probably doing more than one job. Split big components into small, reusable parts: UserAvatar, UserInfo and UserActions instead of one UserProfile. Line count isn’t a rule, it’s a warning sign: if you can’t describe the component in one sentence, it’s time to split it.

2. Separate logic with custom hooks

Keep business logic out of the interface. Hooks like useAuth, useFetch and useLocalStorage make testing easier and cut repetition. Instead of fiddling with localStorage in every component, write one useLocalStorage.

3. Put state in the right place

Passing props three or four layers down creates a mess. First move state closer to where it’s needed, or solve it with component composition (children). If necessary, reach for Context or a light library like Zustand. But don’t put everything in Context; it causes needless re-renders.

4. Folder structure saves you

src/
├── components/   # reusable pieces
├── pages/        # page components
├── hooks/        # custom hooks
├── services/     # API calls
├── utils/        # helper functions
└── context/      # context providers

A feature-based layout is a good alternative: features/auth/, features/dashboard/. Whichever you choose, make sure the whole team knows the same structure.

5. Treat useEffect as a last resort

useEffect is one of the most common sources of bugs in React. Don’t copy a derivable value into state inside an effect; compute it during render. Do work triggered by user interaction in an event handler. Use effects only to synchronise with external systems (network, DOM, subscriptions). Keep the dependency array honest and leave the exhaustive-deps rule on.

6. Type safety: TypeScript

In large projects, TypeScript makes refactoring safe and catches bugs at compile time. propTypes checks no longer run in React 19, so if you want type safety, TypeScript is the sensible way to go.

7. Build reusable components

Instead of writing the same button in five places, write one general Button customised through props like variant and size. Learn the compound components pattern: <Select> and <Select.Option> are designed together.

8. Measure first, then optimise

Premature optimisation is bad. Find the bottleneck with the React DevTools Profiler first. Then, if needed, use React.memo, useMemo, useCallback and code splitting. Optimisation without measurement mostly adds complexity.

9. Write tests from the user’s point of view

Test components with React Testing Library the way a user sees them: query by role, label and text. Tests give you the courage to refactor. If you can’t test everything, at least cover the critical flows.

10. Trust ESLint and Prettier

Prettier for consistency, ESLint for quality. Keep the eslint-plugin-react-hooks rules on. Add Husky and lint-staged for automatic checks before commits.

Conclusion: clean code is an investment

Keeping React code clean is hard, yes; at first it seems to slow you down. But over time the discipline makes life easier for you and your team. Software isn’t only about producing something that works; it’s about leaving a structure others can maintain.

↑↓ move · ↵ select · esc close