Clean React 19: Extracting Logic into useSetup Hooks and Isolating TestsArticle · digest for October 20
Still life of an engineering desk with a useSetup architecture notebook and a monitor running fast isolated React 19 component tests.
Article2 sections≈ 4 min

Clean React 19: Extracting Logic into useSetup Hooks and Isolating Tests

How many times have you opened a React component at work and found a chaotic soup of a dozen useState calls, loading flags, permission checks, and miles of JSX? Even in React 19, components still turn into unwieldy monsters that are painful to maintain and nearly impossible to test cleanly.

When you test a component like that using standard render from React Testing Library, the test dives deep into every child node. One tiny tweak to styles or internal markup in a small card breaks parent panel tests ten screens up.

Software engineer Evgeny Plotnikov (@bad4iz) proposed an elegant way out: the useSetup[Component] architectural pattern, inspired by Vue 3's setup section, paired with Shallowly, a lightweight shallow-rendering library for Vitest.

Separation of Concerns: Hook for Logic, Component for JSX

The idea is straightforward: the component should stay a pure visual function. All data preparation, loading states, and model mapping move into a dedicated hook prefixed with useSetup:

// src/useSetupProfilePanel.ts
import { useProfile } from './useProfile';

export function useSetupProfilePanel() {
  const state = useProfile();

  if (state.status === 'loading') {
    return {
      isLoading: true as const,
      userCardProps: null,
    };
  }

  return {
    isLoading: false as const,
    userCardProps: {
      name: state.profile.name,
    },
  };
}

Now look how clean and readable the component itself becomes:

// src/ProfilePanel.tsx
import { UserCard } from './UserCard';
import { useSetupProfilePanel } from './useSetupProfilePanel';

export function ProfilePanel() {
  const { isLoading, userCardProps } = useSetupProfilePanel();

  if (isLoading) {
    return <p role="status">Loading profile...</p>;
  }

  return (
    <section aria-label="Profile">
      <h1>Your profile</h1>
      <UserCard {...userCardProps} />
    </section>
  );
}

The component no longer knows anything about data sources. It just takes ready-made props and lays them out across the element tree.

A Three-Tier Testing Strategy

With this structure, testing splits into three clear layers with zero redundant checks:

  1. Hook logic test: test useSetupProfilePanel via renderHook with mocked network responses.
  2. Shallow markup test: verify that the parent passes the right props to UserCard and renders accessibility attributes (role, aria-label).
  3. DOM integration test: run only where you need to verify an end-to-end user flow.

For that second layer, Shallowly steps in as a modern replacement for the old Enzyme, built for React 18/19 and Vitest:

// src/ProfilePanel.shallow.test.tsx
import { shallow } from 'shallowly';
import { describe, expect, it, vi } from 'vitest';
import { ProfilePanel } from './ProfilePanel';
import { UserCard } from './UserCard';
import { useSetupProfilePanel } from './useSetupProfilePanel';

vi.mock('./useSetupProfilePanel');

describe('ProfilePanel shallow tests', () => {
  it('passes prepared props to the child card', () => {
    const userCardProps = { name: 'Алексей' };
    useSetupProfilePanel.mockReturnValue({ isLoading: false, userCardProps });

    const wrapper = shallow(<ProfilePanel />);
    expect(wrapper.find(UserCard).props()).toEqual(userCardProps);
  });

  it('renders loading status without rendering the card', () => {
    useSetupProfilePanel.mockReturnValue({ isLoading: true, userCardProps: null });

    const wrapper = shallow(<ProfilePanel />);
    expect(wrapper.find('p').props()).toEqual({
      role: 'status',
      children: 'Loading profile...',
    });
    expect(wrapper.find(UserCard).exists()).toBe(false);
  });
});

In a Shallowly test, UserCard isn't even initialized: its hooks don't run, no network requests fire, and the whole test finishes in milliseconds.

How to Run the Demo Project Locally

To try this approach on Node.js 22 or 24, clone the official repository at the article's tag and run the test suite:

git clone --branch profile-panel-article-v3 --single-branch https://github.com/bad4iz/unit-test-article.git
cd unit-test-article/examples/profile-panel
npm ci && npm test && npm run typecheck

All 6 tests (covering shallow, hooks, and DOM) pass in fractions of a second with zero type errors.

This pattern takes a bit of discipline when creating files, but it pays off right away: your tests stop being brittle, and logic and markup evolve independently.

Want a deep diveVotes help us choose what to break down in detail. This material has 0 votes.
Sources · 2