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
.claude/rules/state-lift-state.mdWhat 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
useEffectthat pushes every keystroke back up to the parent - Stop reading state from a
refon submit - Explain in review why props and refs fail for shared dialog state
Example prompts for the agent
- Refactor
ForwardMessageDialogso state lives inForwardMessageProvider, andForwardButtonpullsactions.submitfromComposer.Context - Remove the
onInputChangeeffect fromForwardMessageComposerand expose state through the provider instead - Check this dialog against the
state-lift-staterule 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.mdThe paths frontmatter field limits the files
AGENTS.md
Codex reads AGENTS.md: paste the rule text there
Manually: put the files in place
.cursor/rules/state-lift-state.mdcFrontmatter: description, globs, alwaysApply
Manually: put the files in place
.github/instructions/state-lift-state.instructions.mdThe applyTo field sets which files it applies to
Manually: put the files in place
.devin/rules/state-lift-state.mdManually: put the files in place
.clinerules/state-lift-state.mdStep 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