How to structure React and TypeScript apps with clear layers and design patterns

Layers give business rules and data access their own places outside the UI. Type checks flag wrong type use before it reaches users.1, 2
Separate views from business logic
React does not impose a code structure. TypeScript adds some discipline, but strong types cannot make up for a messy folder layout.3
A front-end app is JavaScript or TypeScript code that uses React for views. Putting most logic into views or hooks can work in a small app with few business rules.1
As business logic grows, that mixed code takes more effort to understand. Changes also carry more risk when too much logic sits in each component.1
Split view code from the logic that does not draw the UI. Then divide that logic by the role each part plays.1
Keep the UI thin, with business logic in a model layer and data access in another layer. Pure TypeScript domain code need not know that views exist.1
This split lets you change business rules without worrying as much about views. Logic that is not tied to views can also be reused elsewhere.1
Move state handling into custom hooks so each component can focus on what it renders. A hook can act as a state machine behind the view.1
A UI event reaches the hook, which produces a new state and triggers a render. This split can help even when only one component uses the hook.1 Clean Architecture is also an option for front-end projects.4
Type props, state and reusable components
TypeScript helps address weak type safety and unreliable prop checks as apps grow. It finds bugs at compile time, before they affect users.2
Types make code easier to read and help explain what it does. They also flag wrong type use during code changes.2
Describe component props with an interface or a type alias. Props are required by default in both forms.2
Use a type alias when joining types through unions or intersections. Interfaces are often preferred when types need to be extended.2
For instance, requiredFullName must be supplied, while optionalAge can be left out when marked with ?. TypeScript flags a missing required prop at compile time.2
Use discriminated unions to model data that can be loading, an error, or a success. Each state has its own tag, so related state shapes stay distinct.2
The tag helps the component decide which UI to show. Keeping each state shape distinct preserves type safety and makes the logic easier to follow.2
Built-in utility types create new types from ones you already have. Pick, Omit, Partial and Record help shape props and state without repeating type definitions.2
Use typeof and ReturnType to derive types from code or APIs. This cuts manual copying and keeps types in step with the code they describe.2
Generics let a reusable component handle more than one data type safely. The component need not be tied to one fixed data format.2
Choose API patterns by the problem they solve
Use an Adapter when an external API returns data in a format your app does not expect. It maps that response into the format your app uses.5
This keeps domain models apart from external APIs. Your internal interface can stay consistent even when those APIs are not.5
Use a Bridge when the app must support more than one backend through a shared interface. REST and GraphQL services can sit behind the same service contract.5
The shared UserService interface defines a getUser method. Code can call that method with either the REST or GraphQL service.5
Use a Decorator to wrap API calls with logging, caching or retries. The wrapper adds that work without changing the core service logic.5
Use an Observer when several parts of an app need to react to server updates. It supports updates from API polling or WebSocket events.5
Listeners subscribe to receive data when the observer sends an update. They can also unsubscribe when they no longer need those updates.5
Keep API calls in their own service modules. Use React hooks to hold state for the data those calls return.5
The useUser hook calls userService.getUser and stores the result in state. The service handles the request, while the hook returns the current user data.5
Web development services at Kraftwire
Kraftwire offers custom software development. Kraftwire also offers website development.
Kraftwire offers API development. Cloud solutions are also part of Kraftwire's services.
Kraftwire uses an AI digital engineering system to turn year-long projects into six-week deliverables. The workflow pairs developers with language models to speed up the work.6
Kraftwire starts with goals, constraints and a clear view of what done means. Design approval comes before development begins.6
Kraftwire's development stage includes clean code, regular updates and working software clients can see. Launch and support include deployment, monitoring and ongoing help.6
Sources
FAQ
Why should React be treated as just a view layer rather than the entire application?
Mixing business logic into components makes growing code harder to understand and riskier to change. Separating views from business logic lets each change with less impact on the other and makes the logic easier to reuse.
How do discriminated unions assist conditional rendering in React components?
They give loading, error and success states distinct tags. Those tags help components choose which UI to show while keeping state type-safe.
How do Adapter and Bridge patterns help with API integration in TypeScript?
An Adapter maps external API responses into the format your app expects. A Bridge puts backends such as REST and GraphQL behind a shared service interface.