--- name: react-composition description: Build composable React components using Effect Atom for state management. Use this skill when implementing React UIs that avoid boolean props, embrace component composition, and integrate with Effect's reactive state system. --- # React Composition Skill Build React UIs using compositional patterns, Effect Atom for state management, and the component module pattern. Use this skill when creating React applications that integrate with Effect's ecosystem. ## When to Use This Skill - Building React components that integrate with Effect Atom state - Refactoring components away from boolean prop anti-patterns - Implementing complex UIs through composition of simple pieces - Creating reusable component libraries with flexible APIs - Managing shared state across multiple React components - Lifting state to appropriate levels in component trees ## Core Principles ### 1. Composition over Configuration Build complex UIs from simple, composable pieces rather than configuring behavior through props. **Anti-Pattern: Boolean Props** ```tsx // L WRONG - Configuration through boolean props interface FormProps { isUpdate?: boolean hideWelcome?: boolean showEmail?: boolean redirectOnSuccess?: boolean enableValidation?: boolean } function UserForm({ isUpdate, hideWelcome, showEmail, redirectOnSuccess, enableValidation }: FormProps) { return (
{!hideWelcome && } {showEmail && } ) } // Usage becomes unreadable ``` **Correct Pattern: Composition** ```tsx //  CORRECT - Compose specific forms from atomic pieces export namespace UserForm { export const Frame: React.FC<{ children: React.ReactNode }> = ({ children }) =>
{children}
export const WelcomeMessage: React.FC = () =>
Welcome!
export const NameField: React.FC = () => export const EmailField: React.FC = () => export const SubmitButton: React.FC<{ children: React.ReactNode }> = ({ children }) => } // Create specific forms through composition function CreateUserForm() { return ( Create ) } function UpdateUserForm() { return ( Update ) } // Usage is clear and explicit (in JSX context): // // ``` ### 2. Component Module Pattern Treat components like Effect modules with namespace imports and exported sub-components. ```tsx // components/Composer/Composer.tsx import * as React from "react" /** * Composer state interface */ export interface ComposerState { readonly content: string readonly attachments: ReadonlyArray readonly isSubmitting: boolean } /** * Composer context for sharing state */ const ComposerContext = React.createContext(null) /** * Hook to access composer state * @throws when used outside Provider */ export const useComposer = (): ComposerState => { const context = React.useContext(ComposerContext) if (!context) { throw new Error("useComposer must be used within Composer.Provider") } return context } /** * Provider component for composer state */ export const Provider: React.FC<{ children: React.ReactNode state: ComposerState }> = ({ children, state }) => ( {children} ) /** * Frame component for layout */ export const Frame: React.FC<{ children: React.ReactNode }> = ({ children }) => (
{children}
) /** * Input component for message content */ export const Input: React.FC = () => { const { content } = useComposer() return (