React Architecture: 3 Practices for Clear UI & State
In this article
To build better React apps, adopt three architecture practices that keep UI and state clear: colocate state with the components that use it, use a unidirectional data flow with explicit actions, and separate business logic from presentation components. These practices reduce bugs, make code easier to maintain, and scale well as your app grows.
in t | Finish time | min | 50 | 0 in d | Distance | km | 10 | 0 out pace | Average pace | min/km | t / d | 2 out speed | Average speed | km/h | d / (t / 60) | 2
Formula
The calculator computes average pace and speed from a finish time and distance. The formulas are:
- Pace = finish time (minutes) ÷ distance (km). Result is in minutes per kilometer.
- Speed = distance (km) ÷ (finish time ÷ 60). Result is in kilometers per hour.
Variables: t = finish time in minutes, d = distance in kilometers. Pace is expressed as min/km, speed as km/h.
Worked Example
Using the default values: finish time = 50 minutes, distance = 10 km.
- Pace = 50 ÷ 10 = 5.00 min/km.
- Speed = 10 ÷ (50/60) = 10 ÷ 0.8333 = 12.00 km/h.
The calculator shows 5.00 min/km and 12.00 km/h, matching the formula output.
Why These Practices Matter
React itself doesn't enforce a specific architecture. Without deliberate structure, state can end up scattered, components become tangled, and debugging turns into a hunt through props. The three practices below form a pragmatic approach that works for small apps and scales to large ones.
1. Colocate State with Components
Keep state as close as possible to the components that use it. If only one component needs a piece of data, store it in that component's local state. If a few child components share data, lift the state to their nearest common parent. This avoids unnecessary global state and reduces re-renders.
For example, a form input's value should live in the form component, not in a global store. If you later need the value elsewhere, you can lift it up or use a context—but only when needed.
2. Unidirectional Data Flow with Explicit Actions
Data should flow down via props and changes should flow up via callbacks. This pattern makes the flow predictable. When a child wants to change state, it calls a function passed from the parent. This is especially clear when you name actions after what they do, like onSubmit or onToggle.
For complex state transitions, use a reducer (like useReducer) to centralize logic. This makes state changes testable and traceable.
3. Separate Business Logic from Presentation
Split components into two categories: presentational (dumb) components that only render props, and container (smart) components that manage state and business logic. This separation lets you reuse presentational components easily and test logic independently.
You can also extract logic into custom hooks. A hook like useFetch can handle data fetching and loading states, keeping components clean.
How to Choose the Right Architecture
Start with local state and lift it only when necessary. Use Context for global data that many components read (like theme), but avoid using it for frequently changing data. For large apps, consider a state management library like Redux or Zustand, but only when the complexity justifies it.
Ask yourself: Does the state need to be shared? If not, keep it local. If yes, is the sharing limited to a few components? Then lift it. Only if it's app-wide and complex should you reach for a global store.
Common Problems and Mistakes
- Overusing global state: Putting everything in Redux makes debugging harder and causes unnecessary re-renders.
- Prop drilling: Passing props through many levels can be solved with composition or Context, but don't overuse Context either.
- Mutating state directly: Always use setState or dispatch to update state; direct mutation breaks React's reactivity.
- Mixing logic and UI: When components become too large, split them into presentational and container parts.
Comparison with Other Approaches
React's built-in state management (useState, useReducer, Context) is sufficient for many apps. Libraries like Redux add predictability and devtools but introduce boilerplate. Zustand offers a simpler API with less boilerplate. The choice depends on team familiarity and app complexity.
For most new projects, starting with built-in tools and adding a library only when needed is a sound approach.
Frequently Asked Questions
What is the difference between state and props?
Props are read-only data passed from parent to child. State is data that a component owns and can change. Changing state triggers a re-render; changing props does not (unless the parent re-renders).
When should I use Context vs. Redux?
Context is built-in and good for low-frequency updates like theme or user info. Redux is better for high-frequency updates or complex state logic, as it provides optimizations and middleware. Use Context first; if performance becomes an issue, consider Redux.
How do I avoid prop drilling?
Use composition: pass components as props instead of data. Or use Context for values that many components need. But avoid using Context for everything; it can make components less reusable.
Can I use these practices in a small app?
Yes, they are beneficial even in small apps. They keep code organized and make future changes easier. You don't need a heavy library; just follow the principles.
What is a custom hook and how does it help?
A custom hook is a function that uses React hooks to encapsulate reusable logic. For example, a useLocalStorage hook can sync state with localStorage. This keeps components clean and logic testable.