DEV Community

Sherotree
Sherotree

Posted on

Stop the agent from wrapping a leaf component in a new React context provider

The ticket is “remember the last selected tab.” The agent adds TabMemoryContext, a provider in the leaf file, and a hook used by one child. The tree already has a page-level store, a form library, or a simple piece of state in the parent. The new context makes the button harder to test and hides the state from everything else on the page.

I’ve started treating a brand-new provider in a leaf component as a smell. Context is for a real sharing boundary. One child, or one checkbox, is usually a prop or the existing page state.

Why a new provider shows up

The model has been trained on “lift state into context” examples. It will reach for createContext the way it reaches for useMemo: as a default structure, not as a measured need. A provider also looks tidy in a generated file because the state and the consumer travel together.

The cost shows up later. Tests have to wrap the leaf. A sibling that needs the same tab cannot see it. You now have two sources of truth if the page already had a query param or a reducer for the same UI.

A simple state-boundary policy

  • One consumer: use a prop, or state in the parent
  • Reuse the existing page, feature, or session store before adding a context
  • A new provider needs at least two distant consumers, and a name that matches a real boundary
  • Do not nest a new provider inside a component that is itself only used once

URL state and the existing form library count as “already shared.” If the tab is already in the query string, point the agent at that, not at a memory context.

What to put in the prompt

Do not add a new React context or provider for this change.
Use props, the existing page state, or the store already used on this screen (see src/state or the page reducer).
If you believe a new provider is required, stop and explain the sharing boundary first. Do not nest it in a leaf file.
Enter fullscreen mode Exit fullscreen mode

In review, search the diff for createContext, Provider, and useContext. New hits need a reason that is not “the child needed a value.”

Reject / send back

Reject

  • A *Context.tsx created next to a single button
  • A provider wrapped around one return in the same file that consumes it
  • A second theme, user, or toast context when the app already has one
  • Context used only to avoid passing three props

Accept

  • Props or parent state for a local widget
  • A documented addition to the existing feature store
  • A new provider that is a real isolation tier, added at the layout and named on the architecture map

Top comments (0)