Full answer
Folder structure. A layout by type (components/, hooks/, utils/) works for small apps but turns into a place where every change touches ten folders. At scale, group by feature (also called a vertical slice): features/courses/ contains its components, hooks, API functions, types and tests. A feature exposes a small public API through an index file, and other code imports only from that index, not from internal paths. A common layering is shared/ (design system, utilities), features/ (business logic), and app/ or pages/ (routing and composition). Dependencies point one way: shared does not import from features, and features do not import from each other's internals. Lint rules such as eslint-plugin-boundaries, import/no-restricted-paths or Nx module boundaries enforce this automatically. Fewer cross-feature imports make code easier to delete, own and split into packages.
State placement. Ask in order:
- Can it be derived? Compute during render, do not store it.
- Is it server data? Use a query cache (TanStack Query, RTK Query, framework loaders), not hand-copied state.
- Should it be shareable or survive refresh? Put it in the URL (search params).
- Is it used by one component or subtree? Keep it local with
useStateoruseReducerin the lowest component that needs it. - Do distant components need it? Lift it to the nearest common parent, or use context for slow-changing values like theme and user, or a store (Redux Toolkit, Zustand) for frequently changing client state.
Colocation is also a performance technique. When state lives high, each change re-renders everything below it unless memoized. Moving the state down into the component that uses it limits the work. A well-known trick is to move the stateful part into its own component, or pass expensive subtrees as children, which keep their element identity across the parent's state changes.
Trade-offs: feature folders can duplicate small helpers, shared code can become a dumping ground (review what enters shared), and strict boundaries need tooling discipline.
The example compares a lifted state version and a colocated version of the same page, counting renders of an expensive report.
import { memo, useState } from 'react';
const Report = memo(function Report({ tag }) {
console.log(tag + ' report render');
return <p>Report ({tag})</p>;
});
// State sits too high: every keystroke re-renders the whole page, and the Report is re-created.
function LiftedPage() {
const [query, setQuery] = useState('');
return (
<section>
<input aria-label="Lifted search" value={query} onChange={(e) => setQuery(e.target.value)} />
<Report tag="lifted" data={{ query }} />
</section>
);
}
// State lives in the component that uses it. The Report does not depend on it.
function SearchBox() {
const [query, setQuery] = useState('');
return <input aria-label="Colocated search" value={query} onChange={(e) => setQuery(e.target.value)} />;
}
function ColocatedPage() {
return (
<section>
<SearchBox />
<Report tag="colocated" />
</section>
);
}
export default function App() {
return (
<div>
<LiftedPage />
<ColocatedPage />
</div>
);
}In the lifted version the Report receives a new data object on every render, which shows how a parent's state change reaches it even through memo. The check typed three characters into each search box, and the sidecar counted the render messages. Output of the run: report renders on mount: lifted=1, colocated=1 report renders after typing 3 characters in each box: lifted=3, colocated=0
Key points
- Group code by feature with a small public index, and enforce import directions with lint rules.
- Layer shared, features and app. Dependencies point one way.
- Decide state placement in order: derive, server cache, URL, local, lifted or global.
- Keep state in the lowest component that needs it. Colocation limits re-renders.
- Review shared folders so they do not become dumping grounds.
Common mistakes
- Organizing only by file type in a large codebase.
- Importing deep paths of another feature instead of its public index.
- Lifting all state to the top "just in case".
- Copying server data into local state.
- Letting shared folders collect business logic.
Follow-ups
- How would you enforce module boundaries automatically?
- When would you split a feature into its own package or micro-frontend?
- How does passing children help avoid re-renders?
Tips
- Give the ordered state decision list. It is easy to remember and defend.
- Mention automatic boundary enforcement. It separates convention from practice.
- Show that colocation is a performance tool.