@0xsown/vibe-code-fe 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/index.js +181 -0
- package/package.json +32 -0
- package/skills/claude-md-improver/SKILL.md +179 -0
- package/skills/claude-md-improver/references/quality-criteria.md +109 -0
- package/skills/claude-md-improver/references/templates.md +253 -0
- package/skills/claude-md-improver/references/update-guidelines.md +150 -0
- package/skills/find-skills/SKILL.md +133 -0
- package/skills/frontend-design/LICENSE.txt +177 -0
- package/skills/frontend-design/SKILL.md +42 -0
- package/skills/next-best-practices/SKILL.md +153 -0
- package/skills/next-best-practices/async-patterns.md +87 -0
- package/skills/next-best-practices/bundling.md +180 -0
- package/skills/next-best-practices/data-patterns.md +297 -0
- package/skills/next-best-practices/debug-tricks.md +105 -0
- package/skills/next-best-practices/directives.md +73 -0
- package/skills/next-best-practices/error-handling.md +227 -0
- package/skills/next-best-practices/file-conventions.md +140 -0
- package/skills/next-best-practices/font.md +245 -0
- package/skills/next-best-practices/functions.md +108 -0
- package/skills/next-best-practices/hydration-error.md +91 -0
- package/skills/next-best-practices/image.md +173 -0
- package/skills/next-best-practices/metadata.md +301 -0
- package/skills/next-best-practices/parallel-routes.md +287 -0
- package/skills/next-best-practices/route-handlers.md +146 -0
- package/skills/next-best-practices/rsc-boundaries.md +159 -0
- package/skills/next-best-practices/runtime-selection.md +39 -0
- package/skills/next-best-practices/scripts.md +141 -0
- package/skills/next-best-practices/self-hosting.md +371 -0
- package/skills/next-best-practices/suspense-boundaries.md +67 -0
- package/skills/next-cache-components/SKILL.md +411 -0
- package/skills/shadcn-ui/README.md +248 -0
- package/skills/shadcn-ui/SKILL.md +326 -0
- package/skills/shadcn-ui/examples/auth-layout.tsx +177 -0
- package/skills/shadcn-ui/examples/data-table.tsx +313 -0
- package/skills/shadcn-ui/examples/form-pattern.tsx +177 -0
- package/skills/shadcn-ui/resources/component-catalog.md +481 -0
- package/skills/shadcn-ui/resources/customization-guide.md +516 -0
- package/skills/shadcn-ui/resources/migration-guide.md +463 -0
- package/skills/shadcn-ui/resources/setup-guide.md +412 -0
- package/skills/shadcn-ui/scripts/verify-setup.sh +134 -0
- package/skills/supabase-postgres-best-practices/AGENTS.md +68 -0
- package/skills/supabase-postgres-best-practices/CLAUDE.md +68 -0
- package/skills/supabase-postgres-best-practices/README.md +116 -0
- package/skills/supabase-postgres-best-practices/SKILL.md +64 -0
- package/skills/supabase-postgres-best-practices/references/advanced-full-text-search.md +55 -0
- package/skills/supabase-postgres-best-practices/references/advanced-jsonb-indexing.md +49 -0
- package/skills/supabase-postgres-best-practices/references/conn-idle-timeout.md +46 -0
- package/skills/supabase-postgres-best-practices/references/conn-limits.md +44 -0
- package/skills/supabase-postgres-best-practices/references/conn-pooling.md +41 -0
- package/skills/supabase-postgres-best-practices/references/conn-prepared-statements.md +46 -0
- package/skills/supabase-postgres-best-practices/references/data-batch-inserts.md +54 -0
- package/skills/supabase-postgres-best-practices/references/data-n-plus-one.md +53 -0
- package/skills/supabase-postgres-best-practices/references/data-pagination.md +50 -0
- package/skills/supabase-postgres-best-practices/references/data-upsert.md +50 -0
- package/skills/supabase-postgres-best-practices/references/lock-advisory.md +56 -0
- package/skills/supabase-postgres-best-practices/references/lock-deadlock-prevention.md +68 -0
- package/skills/supabase-postgres-best-practices/references/lock-short-transactions.md +50 -0
- package/skills/supabase-postgres-best-practices/references/lock-skip-locked.md +54 -0
- package/skills/supabase-postgres-best-practices/references/monitor-explain-analyze.md +45 -0
- package/skills/supabase-postgres-best-practices/references/monitor-pg-stat-statements.md +55 -0
- package/skills/supabase-postgres-best-practices/references/monitor-vacuum-analyze.md +55 -0
- package/skills/supabase-postgres-best-practices/references/query-composite-indexes.md +44 -0
- package/skills/supabase-postgres-best-practices/references/query-covering-indexes.md +40 -0
- package/skills/supabase-postgres-best-practices/references/query-index-types.md +48 -0
- package/skills/supabase-postgres-best-practices/references/query-missing-indexes.md +43 -0
- package/skills/supabase-postgres-best-practices/references/query-partial-indexes.md +45 -0
- package/skills/supabase-postgres-best-practices/references/schema-constraints.md +80 -0
- package/skills/supabase-postgres-best-practices/references/schema-data-types.md +46 -0
- package/skills/supabase-postgres-best-practices/references/schema-foreign-key-indexes.md +59 -0
- package/skills/supabase-postgres-best-practices/references/schema-lowercase-identifiers.md +55 -0
- package/skills/supabase-postgres-best-practices/references/schema-partitioning.md +55 -0
- package/skills/supabase-postgres-best-practices/references/schema-primary-keys.md +61 -0
- package/skills/supabase-postgres-best-practices/references/security-privileges.md +54 -0
- package/skills/supabase-postgres-best-practices/references/security-rls-basics.md +50 -0
- package/skills/supabase-postgres-best-practices/references/security-rls-performance.md +57 -0
- package/skills/tailwind-design-system/SKILL.md +874 -0
- package/skills/vercel-composition-patterns/AGENTS.md +946 -0
- package/skills/vercel-composition-patterns/README.md +60 -0
- package/skills/vercel-composition-patterns/SKILL.md +89 -0
- package/skills/vercel-composition-patterns/rules/architecture-avoid-boolean-props.md +100 -0
- package/skills/vercel-composition-patterns/rules/architecture-compound-components.md +112 -0
- package/skills/vercel-composition-patterns/rules/patterns-children-over-render-props.md +87 -0
- package/skills/vercel-composition-patterns/rules/patterns-explicit-variants.md +100 -0
- package/skills/vercel-composition-patterns/rules/react19-no-forwardref.md +42 -0
- package/skills/vercel-composition-patterns/rules/state-context-interface.md +191 -0
- package/skills/vercel-composition-patterns/rules/state-decouple-implementation.md +113 -0
- package/skills/vercel-composition-patterns/rules/state-lift-state.md +125 -0
- package/skills/vercel-react-best-practices/AGENTS.md +2934 -0
- package/skills/vercel-react-best-practices/README.md +123 -0
- package/skills/vercel-react-best-practices/SKILL.md +136 -0
- package/skills/vercel-react-best-practices/rules/advanced-event-handler-refs.md +55 -0
- package/skills/vercel-react-best-practices/rules/advanced-init-once.md +42 -0
- package/skills/vercel-react-best-practices/rules/advanced-use-latest.md +39 -0
- package/skills/vercel-react-best-practices/rules/async-api-routes.md +38 -0
- package/skills/vercel-react-best-practices/rules/async-defer-await.md +80 -0
- package/skills/vercel-react-best-practices/rules/async-dependencies.md +51 -0
- package/skills/vercel-react-best-practices/rules/async-parallel.md +28 -0
- package/skills/vercel-react-best-practices/rules/async-suspense-boundaries.md +99 -0
- package/skills/vercel-react-best-practices/rules/bundle-barrel-imports.md +59 -0
- package/skills/vercel-react-best-practices/rules/bundle-conditional.md +31 -0
- package/skills/vercel-react-best-practices/rules/bundle-defer-third-party.md +49 -0
- package/skills/vercel-react-best-practices/rules/bundle-dynamic-imports.md +35 -0
- package/skills/vercel-react-best-practices/rules/bundle-preload.md +50 -0
- package/skills/vercel-react-best-practices/rules/client-event-listeners.md +74 -0
- package/skills/vercel-react-best-practices/rules/client-localstorage-schema.md +71 -0
- package/skills/vercel-react-best-practices/rules/client-passive-event-listeners.md +48 -0
- package/skills/vercel-react-best-practices/rules/client-swr-dedup.md +56 -0
- package/skills/vercel-react-best-practices/rules/js-batch-dom-css.md +107 -0
- package/skills/vercel-react-best-practices/rules/js-cache-function-results.md +80 -0
- package/skills/vercel-react-best-practices/rules/js-cache-property-access.md +28 -0
- package/skills/vercel-react-best-practices/rules/js-cache-storage.md +70 -0
- package/skills/vercel-react-best-practices/rules/js-combine-iterations.md +32 -0
- package/skills/vercel-react-best-practices/rules/js-early-exit.md +50 -0
- package/skills/vercel-react-best-practices/rules/js-hoist-regexp.md +45 -0
- package/skills/vercel-react-best-practices/rules/js-index-maps.md +37 -0
- package/skills/vercel-react-best-practices/rules/js-length-check-first.md +49 -0
- package/skills/vercel-react-best-practices/rules/js-min-max-loop.md +82 -0
- package/skills/vercel-react-best-practices/rules/js-set-map-lookups.md +24 -0
- package/skills/vercel-react-best-practices/rules/js-tosorted-immutable.md +57 -0
- package/skills/vercel-react-best-practices/rules/rendering-activity.md +26 -0
- package/skills/vercel-react-best-practices/rules/rendering-animate-svg-wrapper.md +47 -0
- package/skills/vercel-react-best-practices/rules/rendering-conditional-render.md +40 -0
- package/skills/vercel-react-best-practices/rules/rendering-content-visibility.md +38 -0
- package/skills/vercel-react-best-practices/rules/rendering-hoist-jsx.md +46 -0
- package/skills/vercel-react-best-practices/rules/rendering-hydration-no-flicker.md +82 -0
- package/skills/vercel-react-best-practices/rules/rendering-hydration-suppress-warning.md +30 -0
- package/skills/vercel-react-best-practices/rules/rendering-svg-precision.md +28 -0
- package/skills/vercel-react-best-practices/rules/rendering-usetransition-loading.md +75 -0
- package/skills/vercel-react-best-practices/rules/rerender-defer-reads.md +39 -0
- package/skills/vercel-react-best-practices/rules/rerender-dependencies.md +45 -0
- package/skills/vercel-react-best-practices/rules/rerender-derived-state-no-effect.md +40 -0
- package/skills/vercel-react-best-practices/rules/rerender-derived-state.md +29 -0
- package/skills/vercel-react-best-practices/rules/rerender-functional-setstate.md +74 -0
- package/skills/vercel-react-best-practices/rules/rerender-lazy-state-init.md +58 -0
- package/skills/vercel-react-best-practices/rules/rerender-memo-with-default-value.md +38 -0
- package/skills/vercel-react-best-practices/rules/rerender-memo.md +44 -0
- package/skills/vercel-react-best-practices/rules/rerender-move-effect-to-event.md +45 -0
- package/skills/vercel-react-best-practices/rules/rerender-simple-expression-in-memo.md +35 -0
- package/skills/vercel-react-best-practices/rules/rerender-transitions.md +40 -0
- package/skills/vercel-react-best-practices/rules/rerender-use-ref-transient-values.md +73 -0
- package/skills/vercel-react-best-practices/rules/server-after-nonblocking.md +73 -0
- package/skills/vercel-react-best-practices/rules/server-auth-actions.md +96 -0
- package/skills/vercel-react-best-practices/rules/server-cache-lru.md +41 -0
- package/skills/vercel-react-best-practices/rules/server-cache-react.md +76 -0
- package/skills/vercel-react-best-practices/rules/server-dedup-props.md +65 -0
- package/skills/vercel-react-best-practices/rules/server-parallel-fetching.md +83 -0
- package/skills/vercel-react-best-practices/rules/server-serialization.md +38 -0
- package/skills/vercel-react-native-skills/AGENTS.md +2897 -0
- package/skills/vercel-react-native-skills/README.md +165 -0
- package/skills/vercel-react-native-skills/SKILL.md +121 -0
- package/skills/vercel-react-native-skills/rules/animation-derived-value.md +53 -0
- package/skills/vercel-react-native-skills/rules/animation-gesture-detector-press.md +95 -0
- package/skills/vercel-react-native-skills/rules/animation-gpu-properties.md +65 -0
- package/skills/vercel-react-native-skills/rules/design-system-compound-components.md +66 -0
- package/skills/vercel-react-native-skills/rules/fonts-config-plugin.md +71 -0
- package/skills/vercel-react-native-skills/rules/imports-design-system-folder.md +68 -0
- package/skills/vercel-react-native-skills/rules/js-hoist-intl.md +61 -0
- package/skills/vercel-react-native-skills/rules/list-performance-callbacks.md +44 -0
- package/skills/vercel-react-native-skills/rules/list-performance-function-references.md +132 -0
- package/skills/vercel-react-native-skills/rules/list-performance-images.md +53 -0
- package/skills/vercel-react-native-skills/rules/list-performance-inline-objects.md +97 -0
- package/skills/vercel-react-native-skills/rules/list-performance-item-expensive.md +94 -0
- package/skills/vercel-react-native-skills/rules/list-performance-item-memo.md +82 -0
- package/skills/vercel-react-native-skills/rules/list-performance-item-types.md +104 -0
- package/skills/vercel-react-native-skills/rules/list-performance-virtualize.md +67 -0
- package/skills/vercel-react-native-skills/rules/monorepo-native-deps-in-app.md +46 -0
- package/skills/vercel-react-native-skills/rules/monorepo-single-dependency-versions.md +63 -0
- package/skills/vercel-react-native-skills/rules/navigation-native-navigators.md +188 -0
- package/skills/vercel-react-native-skills/rules/react-compiler-destructure-functions.md +50 -0
- package/skills/vercel-react-native-skills/rules/react-compiler-reanimated-shared-values.md +48 -0
- package/skills/vercel-react-native-skills/rules/react-state-dispatcher.md +91 -0
- package/skills/vercel-react-native-skills/rules/react-state-fallback.md +56 -0
- package/skills/vercel-react-native-skills/rules/react-state-minimize.md +65 -0
- package/skills/vercel-react-native-skills/rules/rendering-no-falsy-and.md +74 -0
- package/skills/vercel-react-native-skills/rules/rendering-text-in-text-component.md +36 -0
- package/skills/vercel-react-native-skills/rules/scroll-position-no-state.md +82 -0
- package/skills/vercel-react-native-skills/rules/state-ground-truth.md +80 -0
- package/skills/vercel-react-native-skills/rules/ui-expo-image.md +66 -0
- package/skills/vercel-react-native-skills/rules/ui-image-gallery.md +104 -0
- package/skills/vercel-react-native-skills/rules/ui-measure-views.md +78 -0
- package/skills/vercel-react-native-skills/rules/ui-menus.md +174 -0
- package/skills/vercel-react-native-skills/rules/ui-native-modals.md +77 -0
- package/skills/vercel-react-native-skills/rules/ui-pressable.md +61 -0
- package/skills/vercel-react-native-skills/rules/ui-safe-area-scroll.md +65 -0
- package/skills/vercel-react-native-skills/rules/ui-scrollview-content-inset.md +45 -0
- package/skills/vercel-react-native-skills/rules/ui-styling.md +87 -0
- package/skills/web-design-guidelines/SKILL.md +39 -0
- package/templates/AGENTS.md +31 -0
- package/templates/CLAUDE.md +31 -0
|
@@ -0,0 +1,946 @@
|
|
|
1
|
+
# React Composition Patterns
|
|
2
|
+
|
|
3
|
+
**Version 1.0.0**
|
|
4
|
+
Engineering
|
|
5
|
+
January 2026
|
|
6
|
+
|
|
7
|
+
> **Note:**
|
|
8
|
+
> This document is mainly for agents and LLMs to follow when maintaining,
|
|
9
|
+
> generating, or refactoring React codebases using composition. Humans
|
|
10
|
+
> may also find it useful, but guidance here is optimized for automation
|
|
11
|
+
> and consistency by AI-assisted workflows.
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## Abstract
|
|
16
|
+
|
|
17
|
+
Composition patterns for building flexible, maintainable React components. Avoid boolean prop proliferation by using compound components, lifting state, and composing internals. These patterns make codebases easier for both humans and AI agents to work with as they scale.
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
## Table of Contents
|
|
22
|
+
|
|
23
|
+
1. [Component Architecture](#1-component-architecture) — **HIGH**
|
|
24
|
+
- 1.1 [Avoid Boolean Prop Proliferation](#11-avoid-boolean-prop-proliferation)
|
|
25
|
+
- 1.2 [Use Compound Components](#12-use-compound-components)
|
|
26
|
+
2. [State Management](#2-state-management) — **MEDIUM**
|
|
27
|
+
- 2.1 [Decouple State Management from UI](#21-decouple-state-management-from-ui)
|
|
28
|
+
- 2.2 [Define Generic Context Interfaces for Dependency Injection](#22-define-generic-context-interfaces-for-dependency-injection)
|
|
29
|
+
- 2.3 [Lift State into Provider Components](#23-lift-state-into-provider-components)
|
|
30
|
+
3. [Implementation Patterns](#3-implementation-patterns) — **MEDIUM**
|
|
31
|
+
- 3.1 [Create Explicit Component Variants](#31-create-explicit-component-variants)
|
|
32
|
+
- 3.2 [Prefer Composing Children Over Render Props](#32-prefer-composing-children-over-render-props)
|
|
33
|
+
4. [React 19 APIs](#4-react-19-apis) — **MEDIUM**
|
|
34
|
+
- 4.1 [React 19 API Changes](#41-react-19-api-changes)
|
|
35
|
+
|
|
36
|
+
---
|
|
37
|
+
|
|
38
|
+
## 1. Component Architecture
|
|
39
|
+
|
|
40
|
+
**Impact: HIGH**
|
|
41
|
+
|
|
42
|
+
Fundamental patterns for structuring components to avoid prop
|
|
43
|
+
proliferation and enable flexible composition.
|
|
44
|
+
|
|
45
|
+
### 1.1 Avoid Boolean Prop Proliferation
|
|
46
|
+
|
|
47
|
+
**Impact: CRITICAL (prevents unmaintainable component variants)**
|
|
48
|
+
|
|
49
|
+
Don't add boolean props like `isThread`, `isEditing`, `isDMThread` to customize
|
|
50
|
+
|
|
51
|
+
component behavior. Each boolean doubles possible states and creates
|
|
52
|
+
|
|
53
|
+
unmaintainable conditional logic. Use composition instead.
|
|
54
|
+
|
|
55
|
+
**Incorrect: boolean props create exponential complexity**
|
|
56
|
+
|
|
57
|
+
```tsx
|
|
58
|
+
function Composer({
|
|
59
|
+
onSubmit,
|
|
60
|
+
isThread,
|
|
61
|
+
channelId,
|
|
62
|
+
isDMThread,
|
|
63
|
+
dmId,
|
|
64
|
+
isEditing,
|
|
65
|
+
isForwarding,
|
|
66
|
+
}: Props) {
|
|
67
|
+
return (
|
|
68
|
+
<form>
|
|
69
|
+
<Header />
|
|
70
|
+
<Input />
|
|
71
|
+
{isDMThread ? (
|
|
72
|
+
<AlsoSendToDMField id={dmId} />
|
|
73
|
+
) : isThread ? (
|
|
74
|
+
<AlsoSendToChannelField id={channelId} />
|
|
75
|
+
) : null}
|
|
76
|
+
{isEditing ? (
|
|
77
|
+
<EditActions />
|
|
78
|
+
) : isForwarding ? (
|
|
79
|
+
<ForwardActions />
|
|
80
|
+
) : (
|
|
81
|
+
<DefaultActions />
|
|
82
|
+
)}
|
|
83
|
+
<Footer onSubmit={onSubmit} />
|
|
84
|
+
</form>
|
|
85
|
+
)
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
**Correct: composition eliminates conditionals**
|
|
90
|
+
|
|
91
|
+
```tsx
|
|
92
|
+
// Channel composer
|
|
93
|
+
function ChannelComposer() {
|
|
94
|
+
return (
|
|
95
|
+
<Composer.Frame>
|
|
96
|
+
<Composer.Header />
|
|
97
|
+
<Composer.Input />
|
|
98
|
+
<Composer.Footer>
|
|
99
|
+
<Composer.Attachments />
|
|
100
|
+
<Composer.Formatting />
|
|
101
|
+
<Composer.Emojis />
|
|
102
|
+
<Composer.Submit />
|
|
103
|
+
</Composer.Footer>
|
|
104
|
+
</Composer.Frame>
|
|
105
|
+
)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Thread composer - adds "also send to channel" field
|
|
109
|
+
function ThreadComposer({ channelId }: { channelId: string }) {
|
|
110
|
+
return (
|
|
111
|
+
<Composer.Frame>
|
|
112
|
+
<Composer.Header />
|
|
113
|
+
<Composer.Input />
|
|
114
|
+
<AlsoSendToChannelField id={channelId} />
|
|
115
|
+
<Composer.Footer>
|
|
116
|
+
<Composer.Formatting />
|
|
117
|
+
<Composer.Emojis />
|
|
118
|
+
<Composer.Submit />
|
|
119
|
+
</Composer.Footer>
|
|
120
|
+
</Composer.Frame>
|
|
121
|
+
)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Edit composer - different footer actions
|
|
125
|
+
function EditComposer() {
|
|
126
|
+
return (
|
|
127
|
+
<Composer.Frame>
|
|
128
|
+
<Composer.Input />
|
|
129
|
+
<Composer.Footer>
|
|
130
|
+
<Composer.Formatting />
|
|
131
|
+
<Composer.Emojis />
|
|
132
|
+
<Composer.CancelEdit />
|
|
133
|
+
<Composer.SaveEdit />
|
|
134
|
+
</Composer.Footer>
|
|
135
|
+
</Composer.Frame>
|
|
136
|
+
)
|
|
137
|
+
}
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
Each variant is explicit about what it renders. We can share internals without
|
|
141
|
+
|
|
142
|
+
sharing a single monolithic parent.
|
|
143
|
+
|
|
144
|
+
### 1.2 Use Compound Components
|
|
145
|
+
|
|
146
|
+
**Impact: HIGH (enables flexible composition without prop drilling)**
|
|
147
|
+
|
|
148
|
+
Structure complex components as compound components with a shared context. Each
|
|
149
|
+
|
|
150
|
+
subcomponent accesses shared state via context, not props. Consumers compose the
|
|
151
|
+
|
|
152
|
+
pieces they need.
|
|
153
|
+
|
|
154
|
+
**Incorrect: monolithic component with render props**
|
|
155
|
+
|
|
156
|
+
```tsx
|
|
157
|
+
function Composer({
|
|
158
|
+
renderHeader,
|
|
159
|
+
renderFooter,
|
|
160
|
+
renderActions,
|
|
161
|
+
showAttachments,
|
|
162
|
+
showFormatting,
|
|
163
|
+
showEmojis,
|
|
164
|
+
}: Props) {
|
|
165
|
+
return (
|
|
166
|
+
<form>
|
|
167
|
+
{renderHeader?.()}
|
|
168
|
+
<Input />
|
|
169
|
+
{showAttachments && <Attachments />}
|
|
170
|
+
{renderFooter ? (
|
|
171
|
+
renderFooter()
|
|
172
|
+
) : (
|
|
173
|
+
<Footer>
|
|
174
|
+
{showFormatting && <Formatting />}
|
|
175
|
+
{showEmojis && <Emojis />}
|
|
176
|
+
{renderActions?.()}
|
|
177
|
+
</Footer>
|
|
178
|
+
)}
|
|
179
|
+
</form>
|
|
180
|
+
)
|
|
181
|
+
}
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
**Correct: compound components with shared context**
|
|
185
|
+
|
|
186
|
+
```tsx
|
|
187
|
+
const ComposerContext = createContext<ComposerContextValue | null>(null)
|
|
188
|
+
|
|
189
|
+
function ComposerProvider({ children, state, actions, meta }: ProviderProps) {
|
|
190
|
+
return (
|
|
191
|
+
<ComposerContext value={{ state, actions, meta }}>
|
|
192
|
+
{children}
|
|
193
|
+
</ComposerContext>
|
|
194
|
+
)
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function ComposerFrame({ children }: { children: React.ReactNode }) {
|
|
198
|
+
return <form>{children}</form>
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function ComposerInput() {
|
|
202
|
+
const {
|
|
203
|
+
state,
|
|
204
|
+
actions: { update },
|
|
205
|
+
meta: { inputRef },
|
|
206
|
+
} = use(ComposerContext)
|
|
207
|
+
return (
|
|
208
|
+
<TextInput
|
|
209
|
+
ref={inputRef}
|
|
210
|
+
value={state.input}
|
|
211
|
+
onChangeText={(text) => update((s) => ({ ...s, input: text }))}
|
|
212
|
+
/>
|
|
213
|
+
)
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function ComposerSubmit() {
|
|
217
|
+
const {
|
|
218
|
+
actions: { submit },
|
|
219
|
+
} = use(ComposerContext)
|
|
220
|
+
return <Button onPress={submit}>Send</Button>
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Export as compound component
|
|
224
|
+
const Composer = {
|
|
225
|
+
Provider: ComposerProvider,
|
|
226
|
+
Frame: ComposerFrame,
|
|
227
|
+
Input: ComposerInput,
|
|
228
|
+
Submit: ComposerSubmit,
|
|
229
|
+
Header: ComposerHeader,
|
|
230
|
+
Footer: ComposerFooter,
|
|
231
|
+
Attachments: ComposerAttachments,
|
|
232
|
+
Formatting: ComposerFormatting,
|
|
233
|
+
Emojis: ComposerEmojis,
|
|
234
|
+
}
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
**Usage:**
|
|
238
|
+
|
|
239
|
+
```tsx
|
|
240
|
+
<Composer.Provider state={state} actions={actions} meta={meta}>
|
|
241
|
+
<Composer.Frame>
|
|
242
|
+
<Composer.Header />
|
|
243
|
+
<Composer.Input />
|
|
244
|
+
<Composer.Footer>
|
|
245
|
+
<Composer.Formatting />
|
|
246
|
+
<Composer.Submit />
|
|
247
|
+
</Composer.Footer>
|
|
248
|
+
</Composer.Frame>
|
|
249
|
+
</Composer.Provider>
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
Consumers explicitly compose exactly what they need. No hidden conditionals. And the state, actions and meta are dependency-injected by a parent provider, allowing multiple usages of the same component structure.
|
|
253
|
+
|
|
254
|
+
---
|
|
255
|
+
|
|
256
|
+
## 2. State Management
|
|
257
|
+
|
|
258
|
+
**Impact: MEDIUM**
|
|
259
|
+
|
|
260
|
+
Patterns for lifting state and managing shared context across
|
|
261
|
+
composed components.
|
|
262
|
+
|
|
263
|
+
### 2.1 Decouple State Management from UI
|
|
264
|
+
|
|
265
|
+
**Impact: MEDIUM (enables swapping state implementations without changing UI)**
|
|
266
|
+
|
|
267
|
+
The provider component should be the only place that knows how state is managed.
|
|
268
|
+
|
|
269
|
+
UI components consume the context interface—they don't know if state comes from
|
|
270
|
+
|
|
271
|
+
useState, Zustand, or a server sync.
|
|
272
|
+
|
|
273
|
+
**Incorrect: UI coupled to state implementation**
|
|
274
|
+
|
|
275
|
+
```tsx
|
|
276
|
+
function ChannelComposer({ channelId }: { channelId: string }) {
|
|
277
|
+
// UI component knows about global state implementation
|
|
278
|
+
const state = useGlobalChannelState(channelId)
|
|
279
|
+
const { submit, updateInput } = useChannelSync(channelId)
|
|
280
|
+
|
|
281
|
+
return (
|
|
282
|
+
<Composer.Frame>
|
|
283
|
+
<Composer.Input
|
|
284
|
+
value={state.input}
|
|
285
|
+
onChange={(text) => sync.updateInput(text)}
|
|
286
|
+
/>
|
|
287
|
+
<Composer.Submit onPress={() => sync.submit()} />
|
|
288
|
+
</Composer.Frame>
|
|
289
|
+
)
|
|
290
|
+
}
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
**Correct: state management isolated in provider**
|
|
294
|
+
|
|
295
|
+
```tsx
|
|
296
|
+
// Provider handles all state management details
|
|
297
|
+
function ChannelProvider({
|
|
298
|
+
channelId,
|
|
299
|
+
children,
|
|
300
|
+
}: {
|
|
301
|
+
channelId: string
|
|
302
|
+
children: React.ReactNode
|
|
303
|
+
}) {
|
|
304
|
+
const { state, update, submit } = useGlobalChannel(channelId)
|
|
305
|
+
const inputRef = useRef(null)
|
|
306
|
+
|
|
307
|
+
return (
|
|
308
|
+
<Composer.Provider
|
|
309
|
+
state={state}
|
|
310
|
+
actions={{ update, submit }}
|
|
311
|
+
meta={{ inputRef }}
|
|
312
|
+
>
|
|
313
|
+
{children}
|
|
314
|
+
</Composer.Provider>
|
|
315
|
+
)
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// UI component only knows about the context interface
|
|
319
|
+
function ChannelComposer() {
|
|
320
|
+
return (
|
|
321
|
+
<Composer.Frame>
|
|
322
|
+
<Composer.Header />
|
|
323
|
+
<Composer.Input />
|
|
324
|
+
<Composer.Footer>
|
|
325
|
+
<Composer.Submit />
|
|
326
|
+
</Composer.Footer>
|
|
327
|
+
</Composer.Frame>
|
|
328
|
+
)
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// Usage
|
|
332
|
+
function Channel({ channelId }: { channelId: string }) {
|
|
333
|
+
return (
|
|
334
|
+
<ChannelProvider channelId={channelId}>
|
|
335
|
+
<ChannelComposer />
|
|
336
|
+
</ChannelProvider>
|
|
337
|
+
)
|
|
338
|
+
}
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
**Different providers, same UI:**
|
|
342
|
+
|
|
343
|
+
```tsx
|
|
344
|
+
// Local state for ephemeral forms
|
|
345
|
+
function ForwardMessageProvider({ children }) {
|
|
346
|
+
const [state, setState] = useState(initialState)
|
|
347
|
+
const forwardMessage = useForwardMessage()
|
|
348
|
+
|
|
349
|
+
return (
|
|
350
|
+
<Composer.Provider
|
|
351
|
+
state={state}
|
|
352
|
+
actions={{ update: setState, submit: forwardMessage }}
|
|
353
|
+
>
|
|
354
|
+
{children}
|
|
355
|
+
</Composer.Provider>
|
|
356
|
+
)
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
// Global synced state for channels
|
|
360
|
+
function ChannelProvider({ channelId, children }) {
|
|
361
|
+
const { state, update, submit } = useGlobalChannel(channelId)
|
|
362
|
+
|
|
363
|
+
return (
|
|
364
|
+
<Composer.Provider state={state} actions={{ update, submit }}>
|
|
365
|
+
{children}
|
|
366
|
+
</Composer.Provider>
|
|
367
|
+
)
|
|
368
|
+
}
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
The same `Composer.Input` component works with both providers because it only
|
|
372
|
+
|
|
373
|
+
depends on the context interface, not the implementation.
|
|
374
|
+
|
|
375
|
+
### 2.2 Define Generic Context Interfaces for Dependency Injection
|
|
376
|
+
|
|
377
|
+
**Impact: HIGH (enables dependency-injectable state across use-cases)**
|
|
378
|
+
|
|
379
|
+
Define a **generic interface** for your component context with three parts:
|
|
380
|
+
|
|
381
|
+
`state`, `actions`, and `meta`. This interface is a contract that any provider
|
|
382
|
+
|
|
383
|
+
can implement—enabling the same UI components to work with completely different
|
|
384
|
+
|
|
385
|
+
state implementations.
|
|
386
|
+
|
|
387
|
+
**Core principle:** Lift state, compose internals, make state
|
|
388
|
+
|
|
389
|
+
dependency-injectable.
|
|
390
|
+
|
|
391
|
+
**Incorrect: UI coupled to specific state implementation**
|
|
392
|
+
|
|
393
|
+
```tsx
|
|
394
|
+
function ComposerInput() {
|
|
395
|
+
// Tightly coupled to a specific hook
|
|
396
|
+
const { input, setInput } = useChannelComposerState()
|
|
397
|
+
return <TextInput value={input} onChangeText={setInput} />
|
|
398
|
+
}
|
|
399
|
+
```
|
|
400
|
+
|
|
401
|
+
**Correct: generic interface enables dependency injection**
|
|
402
|
+
|
|
403
|
+
```tsx
|
|
404
|
+
// Define a GENERIC interface that any provider can implement
|
|
405
|
+
interface ComposerState {
|
|
406
|
+
input: string
|
|
407
|
+
attachments: Attachment[]
|
|
408
|
+
isSubmitting: boolean
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
interface ComposerActions {
|
|
412
|
+
update: (updater: (state: ComposerState) => ComposerState) => void
|
|
413
|
+
submit: () => void
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
interface ComposerMeta {
|
|
417
|
+
inputRef: React.RefObject<TextInput>
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
interface ComposerContextValue {
|
|
421
|
+
state: ComposerState
|
|
422
|
+
actions: ComposerActions
|
|
423
|
+
meta: ComposerMeta
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
const ComposerContext = createContext<ComposerContextValue | null>(null)
|
|
427
|
+
```
|
|
428
|
+
|
|
429
|
+
**UI components consume the interface, not the implementation:**
|
|
430
|
+
|
|
431
|
+
```tsx
|
|
432
|
+
function ComposerInput() {
|
|
433
|
+
const {
|
|
434
|
+
state,
|
|
435
|
+
actions: { update },
|
|
436
|
+
meta,
|
|
437
|
+
} = use(ComposerContext)
|
|
438
|
+
|
|
439
|
+
// This component works with ANY provider that implements the interface
|
|
440
|
+
return (
|
|
441
|
+
<TextInput
|
|
442
|
+
ref={meta.inputRef}
|
|
443
|
+
value={state.input}
|
|
444
|
+
onChangeText={(text) => update((s) => ({ ...s, input: text }))}
|
|
445
|
+
/>
|
|
446
|
+
)
|
|
447
|
+
}
|
|
448
|
+
```
|
|
449
|
+
|
|
450
|
+
**Different providers implement the same interface:**
|
|
451
|
+
|
|
452
|
+
```tsx
|
|
453
|
+
// Provider A: Local state for ephemeral forms
|
|
454
|
+
function ForwardMessageProvider({ children }: { children: React.ReactNode }) {
|
|
455
|
+
const [state, setState] = useState(initialState)
|
|
456
|
+
const inputRef = useRef(null)
|
|
457
|
+
const submit = useForwardMessage()
|
|
458
|
+
|
|
459
|
+
return (
|
|
460
|
+
<ComposerContext
|
|
461
|
+
value={{
|
|
462
|
+
state,
|
|
463
|
+
actions: { update: setState, submit },
|
|
464
|
+
meta: { inputRef },
|
|
465
|
+
}}
|
|
466
|
+
>
|
|
467
|
+
{children}
|
|
468
|
+
</ComposerContext>
|
|
469
|
+
)
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
// Provider B: Global synced state for channels
|
|
473
|
+
function ChannelProvider({ channelId, children }: Props) {
|
|
474
|
+
const { state, update, submit } = useGlobalChannel(channelId)
|
|
475
|
+
const inputRef = useRef(null)
|
|
476
|
+
|
|
477
|
+
return (
|
|
478
|
+
<ComposerContext
|
|
479
|
+
value={{
|
|
480
|
+
state,
|
|
481
|
+
actions: { update, submit },
|
|
482
|
+
meta: { inputRef },
|
|
483
|
+
}}
|
|
484
|
+
>
|
|
485
|
+
{children}
|
|
486
|
+
</ComposerContext>
|
|
487
|
+
)
|
|
488
|
+
}
|
|
489
|
+
```
|
|
490
|
+
|
|
491
|
+
**The same composed UI works with both:**
|
|
492
|
+
|
|
493
|
+
```tsx
|
|
494
|
+
// Works with ForwardMessageProvider (local state)
|
|
495
|
+
<ForwardMessageProvider>
|
|
496
|
+
<Composer.Frame>
|
|
497
|
+
<Composer.Input />
|
|
498
|
+
<Composer.Submit />
|
|
499
|
+
</Composer.Frame>
|
|
500
|
+
</ForwardMessageProvider>
|
|
501
|
+
|
|
502
|
+
// Works with ChannelProvider (global synced state)
|
|
503
|
+
<ChannelProvider channelId="abc">
|
|
504
|
+
<Composer.Frame>
|
|
505
|
+
<Composer.Input />
|
|
506
|
+
<Composer.Submit />
|
|
507
|
+
</Composer.Frame>
|
|
508
|
+
</ChannelProvider>
|
|
509
|
+
```
|
|
510
|
+
|
|
511
|
+
**Custom UI outside the component can access state and actions:**
|
|
512
|
+
|
|
513
|
+
```tsx
|
|
514
|
+
function ForwardMessageDialog() {
|
|
515
|
+
return (
|
|
516
|
+
<ForwardMessageProvider>
|
|
517
|
+
<Dialog>
|
|
518
|
+
{/* The composer UI */}
|
|
519
|
+
<Composer.Frame>
|
|
520
|
+
<Composer.Input placeholder="Add a message, if you'd like." />
|
|
521
|
+
<Composer.Footer>
|
|
522
|
+
<Composer.Formatting />
|
|
523
|
+
<Composer.Emojis />
|
|
524
|
+
</Composer.Footer>
|
|
525
|
+
</Composer.Frame>
|
|
526
|
+
|
|
527
|
+
{/* Custom UI OUTSIDE the composer, but INSIDE the provider */}
|
|
528
|
+
<MessagePreview />
|
|
529
|
+
|
|
530
|
+
{/* Actions at the bottom of the dialog */}
|
|
531
|
+
<DialogActions>
|
|
532
|
+
<CancelButton />
|
|
533
|
+
<ForwardButton />
|
|
534
|
+
</DialogActions>
|
|
535
|
+
</Dialog>
|
|
536
|
+
</ForwardMessageProvider>
|
|
537
|
+
)
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
// This button lives OUTSIDE Composer.Frame but can still submit based on its context!
|
|
541
|
+
function ForwardButton() {
|
|
542
|
+
const {
|
|
543
|
+
actions: { submit },
|
|
544
|
+
} = use(ComposerContext)
|
|
545
|
+
return <Button onPress={submit}>Forward</Button>
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
// This preview lives OUTSIDE Composer.Frame but can read composer's state!
|
|
549
|
+
function MessagePreview() {
|
|
550
|
+
const { state } = use(ComposerContext)
|
|
551
|
+
return <Preview message={state.input} attachments={state.attachments} />
|
|
552
|
+
}
|
|
553
|
+
```
|
|
554
|
+
|
|
555
|
+
The provider boundary is what matters—not the visual nesting. Components that
|
|
556
|
+
|
|
557
|
+
need shared state don't have to be inside the `Composer.Frame`. They just need
|
|
558
|
+
|
|
559
|
+
to be within the provider.
|
|
560
|
+
|
|
561
|
+
The `ForwardButton` and `MessagePreview` are not visually inside the composer
|
|
562
|
+
|
|
563
|
+
box, but they can still access its state and actions. This is the power of
|
|
564
|
+
|
|
565
|
+
lifting state into providers.
|
|
566
|
+
|
|
567
|
+
The UI is reusable bits you compose together. The state is dependency-injected
|
|
568
|
+
|
|
569
|
+
by the provider. Swap the provider, keep the UI.
|
|
570
|
+
|
|
571
|
+
### 2.3 Lift State into Provider Components
|
|
572
|
+
|
|
573
|
+
**Impact: HIGH (enables state sharing outside component boundaries)**
|
|
574
|
+
|
|
575
|
+
Move state management into dedicated provider components. This allows sibling
|
|
576
|
+
|
|
577
|
+
components outside the main UI to access and modify state without prop drilling
|
|
578
|
+
|
|
579
|
+
or awkward refs.
|
|
580
|
+
|
|
581
|
+
**Incorrect: state trapped inside component**
|
|
582
|
+
|
|
583
|
+
```tsx
|
|
584
|
+
function ForwardMessageComposer() {
|
|
585
|
+
const [state, setState] = useState(initialState)
|
|
586
|
+
const forwardMessage = useForwardMessage()
|
|
587
|
+
|
|
588
|
+
return (
|
|
589
|
+
<Composer.Frame>
|
|
590
|
+
<Composer.Input />
|
|
591
|
+
<Composer.Footer />
|
|
592
|
+
</Composer.Frame>
|
|
593
|
+
)
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
// Problem: How does this button access composer state?
|
|
597
|
+
function ForwardMessageDialog() {
|
|
598
|
+
return (
|
|
599
|
+
<Dialog>
|
|
600
|
+
<ForwardMessageComposer />
|
|
601
|
+
<MessagePreview /> {/* Needs composer state */}
|
|
602
|
+
<DialogActions>
|
|
603
|
+
<CancelButton />
|
|
604
|
+
<ForwardButton /> {/* Needs to call submit */}
|
|
605
|
+
</DialogActions>
|
|
606
|
+
</Dialog>
|
|
607
|
+
)
|
|
608
|
+
}
|
|
609
|
+
```
|
|
610
|
+
|
|
611
|
+
**Incorrect: useEffect to sync state up**
|
|
612
|
+
|
|
613
|
+
```tsx
|
|
614
|
+
function ForwardMessageDialog() {
|
|
615
|
+
const [input, setInput] = useState('')
|
|
616
|
+
return (
|
|
617
|
+
<Dialog>
|
|
618
|
+
<ForwardMessageComposer onInputChange={setInput} />
|
|
619
|
+
<MessagePreview input={input} />
|
|
620
|
+
</Dialog>
|
|
621
|
+
)
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
function ForwardMessageComposer({ onInputChange }) {
|
|
625
|
+
const [state, setState] = useState(initialState)
|
|
626
|
+
useEffect(() => {
|
|
627
|
+
onInputChange(state.input) // Sync on every change 😬
|
|
628
|
+
}, [state.input])
|
|
629
|
+
}
|
|
630
|
+
```
|
|
631
|
+
|
|
632
|
+
**Incorrect: reading state from ref on submit**
|
|
633
|
+
|
|
634
|
+
```tsx
|
|
635
|
+
function ForwardMessageDialog() {
|
|
636
|
+
const stateRef = useRef(null)
|
|
637
|
+
return (
|
|
638
|
+
<Dialog>
|
|
639
|
+
<ForwardMessageComposer stateRef={stateRef} />
|
|
640
|
+
<ForwardButton onPress={() => submit(stateRef.current)} />
|
|
641
|
+
</Dialog>
|
|
642
|
+
)
|
|
643
|
+
}
|
|
644
|
+
```
|
|
645
|
+
|
|
646
|
+
**Correct: state lifted to provider**
|
|
647
|
+
|
|
648
|
+
```tsx
|
|
649
|
+
function ForwardMessageProvider({ children }: { children: React.ReactNode }) {
|
|
650
|
+
const [state, setState] = useState(initialState)
|
|
651
|
+
const forwardMessage = useForwardMessage()
|
|
652
|
+
const inputRef = useRef(null)
|
|
653
|
+
|
|
654
|
+
return (
|
|
655
|
+
<Composer.Provider
|
|
656
|
+
state={state}
|
|
657
|
+
actions={{ update: setState, submit: forwardMessage }}
|
|
658
|
+
meta={{ inputRef }}
|
|
659
|
+
>
|
|
660
|
+
{children}
|
|
661
|
+
</Composer.Provider>
|
|
662
|
+
)
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
function ForwardMessageDialog() {
|
|
666
|
+
return (
|
|
667
|
+
<ForwardMessageProvider>
|
|
668
|
+
<Dialog>
|
|
669
|
+
<ForwardMessageComposer />
|
|
670
|
+
<MessagePreview /> {/* Custom components can access state and actions */}
|
|
671
|
+
<DialogActions>
|
|
672
|
+
<CancelButton />
|
|
673
|
+
<ForwardButton /> {/* Custom components can access state and actions */}
|
|
674
|
+
</DialogActions>
|
|
675
|
+
</Dialog>
|
|
676
|
+
</ForwardMessageProvider>
|
|
677
|
+
)
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
function ForwardButton() {
|
|
681
|
+
const { actions } = use(Composer.Context)
|
|
682
|
+
return <Button onPress={actions.submit}>Forward</Button>
|
|
683
|
+
}
|
|
684
|
+
```
|
|
685
|
+
|
|
686
|
+
The ForwardButton lives outside the Composer.Frame but still has access to the
|
|
687
|
+
|
|
688
|
+
submit action because it's within the provider. Even though it's a one-off
|
|
689
|
+
|
|
690
|
+
component, it can still access the composer's state and actions from outside the
|
|
691
|
+
|
|
692
|
+
UI itself.
|
|
693
|
+
|
|
694
|
+
**Key insight:** Components that need shared state don't have to be visually
|
|
695
|
+
|
|
696
|
+
nested inside each other—they just need to be within the same provider.
|
|
697
|
+
|
|
698
|
+
---
|
|
699
|
+
|
|
700
|
+
## 3. Implementation Patterns
|
|
701
|
+
|
|
702
|
+
**Impact: MEDIUM**
|
|
703
|
+
|
|
704
|
+
Specific techniques for implementing compound components and
|
|
705
|
+
context providers.
|
|
706
|
+
|
|
707
|
+
### 3.1 Create Explicit Component Variants
|
|
708
|
+
|
|
709
|
+
**Impact: MEDIUM (self-documenting code, no hidden conditionals)**
|
|
710
|
+
|
|
711
|
+
Instead of one component with many boolean props, create explicit variant
|
|
712
|
+
|
|
713
|
+
components. Each variant composes the pieces it needs. The code documents
|
|
714
|
+
|
|
715
|
+
itself.
|
|
716
|
+
|
|
717
|
+
**Incorrect: one component, many modes**
|
|
718
|
+
|
|
719
|
+
```tsx
|
|
720
|
+
// What does this component actually render?
|
|
721
|
+
<Composer
|
|
722
|
+
isThread
|
|
723
|
+
isEditing={false}
|
|
724
|
+
channelId='abc'
|
|
725
|
+
showAttachments
|
|
726
|
+
showFormatting={false}
|
|
727
|
+
/>
|
|
728
|
+
```
|
|
729
|
+
|
|
730
|
+
**Correct: explicit variants**
|
|
731
|
+
|
|
732
|
+
```tsx
|
|
733
|
+
// Immediately clear what this renders
|
|
734
|
+
<ThreadComposer channelId="abc" />
|
|
735
|
+
|
|
736
|
+
// Or
|
|
737
|
+
<EditMessageComposer messageId="xyz" />
|
|
738
|
+
|
|
739
|
+
// Or
|
|
740
|
+
<ForwardMessageComposer messageId="123" />
|
|
741
|
+
```
|
|
742
|
+
|
|
743
|
+
Each implementation is unique, explicit and self-contained. Yet they can each
|
|
744
|
+
|
|
745
|
+
use shared parts.
|
|
746
|
+
|
|
747
|
+
**Implementation:**
|
|
748
|
+
|
|
749
|
+
```tsx
|
|
750
|
+
function ThreadComposer({ channelId }: { channelId: string }) {
|
|
751
|
+
return (
|
|
752
|
+
<ThreadProvider channelId={channelId}>
|
|
753
|
+
<Composer.Frame>
|
|
754
|
+
<Composer.Input />
|
|
755
|
+
<AlsoSendToChannelField channelId={channelId} />
|
|
756
|
+
<Composer.Footer>
|
|
757
|
+
<Composer.Formatting />
|
|
758
|
+
<Composer.Emojis />
|
|
759
|
+
<Composer.Submit />
|
|
760
|
+
</Composer.Footer>
|
|
761
|
+
</Composer.Frame>
|
|
762
|
+
</ThreadProvider>
|
|
763
|
+
)
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
function EditMessageComposer({ messageId }: { messageId: string }) {
|
|
767
|
+
return (
|
|
768
|
+
<EditMessageProvider messageId={messageId}>
|
|
769
|
+
<Composer.Frame>
|
|
770
|
+
<Composer.Input />
|
|
771
|
+
<Composer.Footer>
|
|
772
|
+
<Composer.Formatting />
|
|
773
|
+
<Composer.Emojis />
|
|
774
|
+
<Composer.CancelEdit />
|
|
775
|
+
<Composer.SaveEdit />
|
|
776
|
+
</Composer.Footer>
|
|
777
|
+
</Composer.Frame>
|
|
778
|
+
</EditMessageProvider>
|
|
779
|
+
)
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
function ForwardMessageComposer({ messageId }: { messageId: string }) {
|
|
783
|
+
return (
|
|
784
|
+
<ForwardMessageProvider messageId={messageId}>
|
|
785
|
+
<Composer.Frame>
|
|
786
|
+
<Composer.Input placeholder="Add a message, if you'd like." />
|
|
787
|
+
<Composer.Footer>
|
|
788
|
+
<Composer.Formatting />
|
|
789
|
+
<Composer.Emojis />
|
|
790
|
+
<Composer.Mentions />
|
|
791
|
+
</Composer.Footer>
|
|
792
|
+
</Composer.Frame>
|
|
793
|
+
</ForwardMessageProvider>
|
|
794
|
+
)
|
|
795
|
+
}
|
|
796
|
+
```
|
|
797
|
+
|
|
798
|
+
Each variant is explicit about:
|
|
799
|
+
|
|
800
|
+
- What provider/state it uses
|
|
801
|
+
|
|
802
|
+
- What UI elements it includes
|
|
803
|
+
|
|
804
|
+
- What actions are available
|
|
805
|
+
|
|
806
|
+
No boolean prop combinations to reason about. No impossible states.
|
|
807
|
+
|
|
808
|
+
### 3.2 Prefer Composing Children Over Render Props
|
|
809
|
+
|
|
810
|
+
**Impact: MEDIUM (cleaner composition, better readability)**
|
|
811
|
+
|
|
812
|
+
Use `children` for composition instead of `renderX` props. Children are more
|
|
813
|
+
|
|
814
|
+
readable, compose naturally, and don't require understanding callback
|
|
815
|
+
|
|
816
|
+
signatures.
|
|
817
|
+
|
|
818
|
+
**Incorrect: render props**
|
|
819
|
+
|
|
820
|
+
```tsx
|
|
821
|
+
function Composer({
|
|
822
|
+
renderHeader,
|
|
823
|
+
renderFooter,
|
|
824
|
+
renderActions,
|
|
825
|
+
}: {
|
|
826
|
+
renderHeader?: () => React.ReactNode
|
|
827
|
+
renderFooter?: () => React.ReactNode
|
|
828
|
+
renderActions?: () => React.ReactNode
|
|
829
|
+
}) {
|
|
830
|
+
return (
|
|
831
|
+
<form>
|
|
832
|
+
{renderHeader?.()}
|
|
833
|
+
<Input />
|
|
834
|
+
{renderFooter ? renderFooter() : <DefaultFooter />}
|
|
835
|
+
{renderActions?.()}
|
|
836
|
+
</form>
|
|
837
|
+
)
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
// Usage is awkward and inflexible
|
|
841
|
+
return (
|
|
842
|
+
<Composer
|
|
843
|
+
renderHeader={() => <CustomHeader />}
|
|
844
|
+
renderFooter={() => (
|
|
845
|
+
<>
|
|
846
|
+
<Formatting />
|
|
847
|
+
<Emojis />
|
|
848
|
+
</>
|
|
849
|
+
)}
|
|
850
|
+
renderActions={() => <SubmitButton />}
|
|
851
|
+
/>
|
|
852
|
+
)
|
|
853
|
+
```
|
|
854
|
+
|
|
855
|
+
**Correct: compound components with children**
|
|
856
|
+
|
|
857
|
+
```tsx
|
|
858
|
+
function ComposerFrame({ children }: { children: React.ReactNode }) {
|
|
859
|
+
return <form>{children}</form>
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
function ComposerFooter({ children }: { children: React.ReactNode }) {
|
|
863
|
+
return <footer className='flex'>{children}</footer>
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
// Usage is flexible
|
|
867
|
+
return (
|
|
868
|
+
<Composer.Frame>
|
|
869
|
+
<CustomHeader />
|
|
870
|
+
<Composer.Input />
|
|
871
|
+
<Composer.Footer>
|
|
872
|
+
<Composer.Formatting />
|
|
873
|
+
<Composer.Emojis />
|
|
874
|
+
<SubmitButton />
|
|
875
|
+
</Composer.Footer>
|
|
876
|
+
</Composer.Frame>
|
|
877
|
+
)
|
|
878
|
+
```
|
|
879
|
+
|
|
880
|
+
**When render props are appropriate:**
|
|
881
|
+
|
|
882
|
+
```tsx
|
|
883
|
+
// Render props work well when you need to pass data back
|
|
884
|
+
<List
|
|
885
|
+
data={items}
|
|
886
|
+
renderItem={({ item, index }) => <Item item={item} index={index} />}
|
|
887
|
+
/>
|
|
888
|
+
```
|
|
889
|
+
|
|
890
|
+
Use render props when the parent needs to provide data or state to the child.
|
|
891
|
+
|
|
892
|
+
Use children when composing static structure.
|
|
893
|
+
|
|
894
|
+
---
|
|
895
|
+
|
|
896
|
+
## 4. React 19 APIs
|
|
897
|
+
|
|
898
|
+
**Impact: MEDIUM**
|
|
899
|
+
|
|
900
|
+
React 19+ only. Don't use `forwardRef`; use `use()` instead of `useContext()`.
|
|
901
|
+
|
|
902
|
+
### 4.1 React 19 API Changes
|
|
903
|
+
|
|
904
|
+
**Impact: MEDIUM (cleaner component definitions and context usage)**
|
|
905
|
+
|
|
906
|
+
> **⚠️ React 19+ only.** Skip this if you're on React 18 or earlier.
|
|
907
|
+
|
|
908
|
+
In React 19, `ref` is now a regular prop (no `forwardRef` wrapper needed), and `use()` replaces `useContext()`.
|
|
909
|
+
|
|
910
|
+
**Incorrect: forwardRef in React 19**
|
|
911
|
+
|
|
912
|
+
```tsx
|
|
913
|
+
const ComposerInput = forwardRef<TextInput, Props>((props, ref) => {
|
|
914
|
+
return <TextInput ref={ref} {...props} />
|
|
915
|
+
})
|
|
916
|
+
```
|
|
917
|
+
|
|
918
|
+
**Correct: ref as a regular prop**
|
|
919
|
+
|
|
920
|
+
```tsx
|
|
921
|
+
function ComposerInput({ ref, ...props }: Props & { ref?: React.Ref<TextInput> }) {
|
|
922
|
+
return <TextInput ref={ref} {...props} />
|
|
923
|
+
}
|
|
924
|
+
```
|
|
925
|
+
|
|
926
|
+
**Incorrect: useContext in React 19**
|
|
927
|
+
|
|
928
|
+
```tsx
|
|
929
|
+
const value = useContext(MyContext)
|
|
930
|
+
```
|
|
931
|
+
|
|
932
|
+
**Correct: use instead of useContext**
|
|
933
|
+
|
|
934
|
+
```tsx
|
|
935
|
+
const value = use(MyContext)
|
|
936
|
+
```
|
|
937
|
+
|
|
938
|
+
`use()` can also be called conditionally, unlike `useContext()`.
|
|
939
|
+
|
|
940
|
+
---
|
|
941
|
+
|
|
942
|
+
## References
|
|
943
|
+
|
|
944
|
+
1. [https://react.dev](https://react.dev)
|
|
945
|
+
2. [https://react.dev/learn/passing-data-deeply-with-context](https://react.dev/learn/passing-data-deeply-with-context)
|
|
946
|
+
3. [https://react.dev/reference/react/use](https://react.dev/reference/react/use)
|