Home / Software Development / React Component Architecture Prompt With Hooks and TypeScript

Software Development

React Component Architecture Prompt With Hooks and TypeScript

Design a React feature as small, testable parts: a data hook, a presentational view, typed props and explicit loading, empty and error states.

3 min read · Published October 18, 2026 · Last reviewed October 18, 2026

Components become hard to maintain when data fetching, state and markup live in one place. Splitting them gives you a view that is easy to test and a hook that can be reused.

This prompt asks the model to propose a file structure first, then the typed hook and component, and to cover the states people forget: idle, loading, empty and error. It also asks for an accessibility check and a test plan.

The prompt

Copy and paste
Act as a senior React and TypeScript engineer.

Design this feature as clean, testable components: {FEATURE}

Return:
1. A file tree for the feature.
2. TypeScript types for props and state. Model the data states as a discriminated union (idle, loading, success, empty, error).
3. A custom hook that holds data fetching and state, including request cancellation.
4. A presentational component that receives data through props and has no data fetching.
5. A short accessibility checklist for this feature.
6. A list of tests to write, naming what each test proves.

Constraints:
- React function components and hooks only.
- No new libraries unless you explain why they are necessary.
- Existing stack: {STACK}

What to replace

VariableWhat to put there
{FEATURE}For example "a user search box that shows results as the user types".
{STACK}For example "React 18, TypeScript strict, Vite, Testing Library".

How to use it

  1. Describe one feature at a time, with the user-facing behavior in a sentence or two.
  2. State your stack so the model does not introduce incompatible libraries.
  3. Review the file tree before reading code. If the split looks wrong, ask for a different one.
  4. Check that the view component has no fetching and that every data state is rendered.
  5. Write the suggested tests first for the hook, then for the view.

Worked example

This example was written for this page to show the format. Results from your own model and your own material will differ, so run the prompt yourself before relying on it.

Input

Feature: a user search box that shows results as the user types. Stack: React 18, TypeScript strict, Vite, Testing Library.

Output

features/user-search/
  UserSearch.tsx        (connects hook to view)
  UserSearchView.tsx    (presentational, props only)
  useUserSearch.ts      (data hook)
  types.ts
  UserSearch.test.tsx

type SearchState =
  | { status: 'idle' }
  | { status: 'loading' }
  | { status: 'success'; users: User[] }
  | { status: 'empty' }
  | { status: 'error'; message: string };

export function useUserSearch(query: string): SearchState {
  const [state, setState] = useState<SearchState>({ status: 'idle' });

  useEffect(() => {
    if (query.trim().length < 2) {
      setState({ status: 'idle' });
      return;
    }
    const controller = new AbortController();
    setState({ status: 'loading' });
    fetchUsers(query, controller.signal)
      .then((users) =>
        setState(users.length ? { status: 'success', users } : { status: 'empty' }),
      )
      .catch((error) => {
        if (!controller.signal.aborted) {
          setState({ status: 'error', message: error.message });
        }
      });
    return () => controller.abort();
  }, [query]);

  return state;
}

Notes: add a debounce in the parent so the hook is not called on every keystroke; give the results list an accessible label and announce the result count to screen readers.

Variations

SituationWhat to change
Server componentsAdd "Use React Server Components where appropriate and say which parts must be client components."
Form-heavy featureAsk for form state, validation rules and error messages as a separate hook.
Design systemAdd your component library name and ask the model to reuse its primitives.
RefactoringPaste the existing component and ask for the same structure plus a migration plan in small steps.

Common mistakes

Frequently asked questions

Can I use this with a state library?

Yes. Name the library in the stack and ask the hook to use it instead of local state.

Why a discriminated union for state?

It makes impossible combinations, such as loading together with an error, unrepresentable and helps the compiler catch missing cases.

Does the generated code need review?

Always. Read it, run it and test it as you would any contribution.

More prompts