Skip to main content
rule CX Codex

state-lift-state — Claude Code rule: lift state into providers

state-lift-state — a Claude Code rule that moves React state into a provider component, so siblings outside the main UI read and update it without prop drilling or refs.

MIT ★ 7,191 updated 2026-10-24

Install into Claude Code · Manually: put the files in place All methods and platforms ↓
.claude/rules/state-lift-state.md
Open on GitHub

What state-lift-state does

The rule moves state into a dedicated provider component. Sibling components outside the main UI can then read and update it without prop drilling or refs. The worked example is a forward-message dialog: the composer owns the input, yet ForwardButton still has to fire actions.submit.

The file walks through three dead ends: state locked inside one component, a useEffect that pushes every change upward, and a ref read at submit time. The fix is a ForwardMessageProvider holding useState, useForwardMessage and inputRef, which hands them to Composer.Provider as state, actions and meta.

Context only reaches components inside the same provider. A button can sit in a different branch of the tree and still work, but it gets nothing when no provider wraps it.

What it helps with

  • Move form state into a provider so buttons elsewhere in the tree can trigger submit
  • Drop the useEffect that pushes every keystroke back up to the parent
  • Stop reading state from a ref on submit
  • Explain in review why props and refs fail for shared dialog state

Example prompts for the agent

  • Refactor ForwardMessageDialog so state lives in ForwardMessageProvider, and ForwardButton pulls actions.submit from Composer.Context
  • Remove the onInputChange effect from ForwardMessageComposer and expose state through the provider instead
  • Check this dialog against the state-lift-state rule and rewrite the spots where state is trapped inside a component

How to install state-lift-state

Manually: put the files in place

.claude/rules/state-lift-state.md

The paths frontmatter field limits the files

Step by step, with commands for each platform: AGENTS.md vs CLAUDE.md: what they are and which one to use

Questions about state-lift-state

How do I install state-lift-state in Claude Code?

Place the rule file at .claude/rules/state-lift-state.md in your project.

What does state-lift-state do?

It covers moving state into a provider component: three wrong approaches plus a working Composer.Provider that exposes state, actions and meta to the outside.

Why not sync state upward with useEffect?

The effect routes every change through the parent. A provider keeps the state once, and components read it from context.

Source text

state-lift-state.md · MIT
---
title: Lift State into Provider Components
impact: HIGH
impactDescription: enables state sharing outside component boundaries
tags: composition, state, context, providers
---

## Lift State into Provider Components

Move state management into dedicated provider components. This allows sibling
components outside the main UI to access and modify state without prop drilling
or awkward refs.

**Incorrect (state trapped inside component):**

```tsx
function ForwardMessageComposer() {
  const [state, setState] = useState(initialState)
  const forwardMessage = useForwardMessage()

  return (
    <Composer.Frame>
      <Composer.Input />
      <Composer.Footer />
    </Composer.Frame>
  )
}

// Problem: How does this button access composer state?
function ForwardMessageDialog() {
  return (
    <Dialog>
      <ForwardMessageComposer />
      <MessagePreview /> {/* Needs composer state */}
      <DialogActions>
        <CancelButton />
        <ForwardButton /> {/* Needs to call submit */}
      </DialogActions>
    </Dialog>
  )
}
```

**Incorrect (useEffect to sync state up):**

```tsx
function ForwardMessageDialog() {
  const [input, setInput] = useState('')
  return (
    <Dialog>
      <ForwardMessageComposer onInputChange={setInput} />
      <MessagePreview input={input} />
    </Dialog>
  )
}

function ForwardMessageComposer({ onInputChange }) {
  const [state, setState] = useState(initialState)
  useEffect(() => {
    onInputChange(state.input) // Sync on every change 😬
  }, [state.input])
}
```

**Incorrect (reading state from ref on submit):**

```tsx
function ForwardMessageDialog() {
  const stateRef = useRef(null)
  return (
    <Dialog>
      <ForwardMessageComposer stateRef={stateRef} />
      <ForwardButton onPress={() => submit(stateRef.current)} />
    </Dialog>
  )
}
```

**Correct (state lifted to provider):**

```tsx
function ForwardMessageProvider({ children }: { children: React.ReactNode }) {
  const [state, setState] = useState(initialState)
  const forwardMessage = useForwardMessage()
  const inputRef = useRef(null)

  return (
    <Composer.Provider
      state={state}
      actions={{ update: setState, submit: forwardMessage }}
      meta={{ inputRef }}
    >
      {children}
    </Composer.Provider>
  )
}

function ForwardMessageDialog() {
  return (
    <ForwardMessageProvider>
      <Dialog>
        <ForwardMessageComposer />
        <MessagePreview /> {/* Custom components can access state and actions */}
        <DialogActions>
          <CancelButton />
          <ForwardButton /> {/* Custom components can access state and actions */}
        </DialogActions>
      </Dialog>
    </ForwardMessageProvider>
  )
}

function ForwardButton() {
  const { actions } = use(Composer.Context)
  return <Button onPress={actions.submit}>Forward</Button>
}
```

The ForwardButton lives outside the Composer.Frame but still has access to the
submit action because it's within the provider. Even though it's a one-off
component, it can still access the composer's state and actions from outside the
UI itself.

**Key insight:** Components that need shared state don't have to be visually
nested inside each other—they just need to be within the same provider.

© tech-leads-club · MIT · tech-leads-club/agent-skills

Similar IDE rules

All IDE rules for Frontend and UI →

From the same set130

The whole set →
async-suspense-boundaries async-suspense-boundaries guides Claude Code to place data fetching behind Suspense boundaries so headers, sidebars, and layout shells render without blocking. CX Codex +1 ★ 7,191repo stars architecture-compound-components The architecture-compound-components rule describes splitting complex UI components into pieces that share state through context. It fits components that have accumulated render props and conditional options. CX Codex +1 ★ 7,191repo stars state-decouple-implementation state-decouple-implementation — a Claude Code rule that keeps state management inside providers and exposes only a context interface to UI components. Swap useState for Zustand or a server sync without touching a single UI file. CX Codex +1 ★ 7,191repo stars rerender-functional-setstate The rerender-functional-setstate rule recommends functional state updates when the next React value depends on the current one. It helps callbacks avoid stale state and unnecessary dependencies. CX Codex +1 ★ 7,191repo stars js-batch-dom-css js-batch-dom-css prevents layout thrashing by separating style updates from layout queries. Helps Claude write performant DOM and React code. CX Codex +1 ★ 7,191repo stars docs-writer The docs-writer skill creates, edits, and reviews project documentation. Use it for READMEs, Markdown files, and pages under docs/. CX Codex +1 ★ 7,191repo stars perf-astro The perf-astro Claude Code skill tunes Astro builds with inlined critical CSS, minification, non-blocking fonts and an LCP preload, aiming for 95+ Lighthouse scores. CX Codex +1 ★ 7,191repo stars tactical-ddd Audits domain models for anemia and refactors them into rich aggregates using tactical DDD patterns in Claude Code. CX Codex +1 ★ 7,191repo stars

Project sponsors

Companies that keep this analytics open