--- 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 ( ) } // Problem: How does this button access composer state? function ForwardMessageDialog() { return ( {/* Needs composer state */} {/* Needs to call submit */} ) } ``` **Incorrect (useEffect to sync state up):** ```tsx function ForwardMessageDialog() { const [input, setInput] = useState('') return ( ) } 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 ( submit(stateRef.current)} /> ) } ``` **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 ( {children} ) } function ForwardMessageDialog() { return ( {/* Custom components can access state and actions */} {/* Custom components can access state and actions */} ) } function ForwardButton() { const { actions } = use(Composer.Context) return } ``` 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.