You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
**Reference:** See `packages/patterns/chatbot-list-view.tsx` for the canonical implementation, specifically the `storeCharm` lift and `createChatRecipe` handler. Full details in `HANDLERS.md` under "Storing Pattern Instances in Cell Arrays".
791
791
792
+
#### 6. Not Including All Cells in Derive Dependencies
793
+
794
+
```typescript
795
+
// ❌ WRONG - items is used in callback but not in dependencies
796
+
{derive(itemCount, (count) =>
797
+
count===0? (
798
+
<div>Noitemsyet</div>
799
+
) : (
800
+
<div>
801
+
{items.map((item) => <div>{item.title}</div>)}
802
+
</div>
803
+
)
804
+
)}
805
+
// Error: Shadow ref alias with parent cell not found in current frame
806
+
807
+
// ✅ CORRECT - Include all cells referenced in the callback
// ✅ ALTERNATIVE - Use direct ternary if you don't need derive's reactivity
819
+
{itemCount===0? (
820
+
<div>Noitemsyet</div>
821
+
) : (
822
+
<div>
823
+
{items.map((item) => <div>{item.title}</div>)}
824
+
</div>
825
+
)}
826
+
```
827
+
828
+
**Why this is a pitfall:** When a derive callback closes over cells (references them from the outer scope), those cells must be included in the dependency array. Otherwise, you'll get cryptic "Shadow ref" errors.
829
+
830
+
**Rule:** If your derive callback uses any cells, include them all in the dependency array: `derive([cell1, cell2, ...], ([val1, val2, ...]) => ...)`.
831
+
832
+
**Note:** Values inside derive callbacks are read-only. If you need bidirectional binding (like `$checked`), use cells directly outside the derive instead.
Copy file name to clipboardExpand all lines: docs/common/RECIPES.md
+53-7Lines changed: 53 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -426,11 +426,11 @@ logic.
426
426
});
427
427
```
428
428
429
-
7.**TypeArrayMapParametersasOpaqueRef**: Whenmappingovercellarrayswithbidirectionalbinding, you**must**addthe`OpaqueRef<T>`typeannotation to make it type-check correctly:
429
+
7.**TypeArrayMapParametersWhenNeeded**: Whenmappingovercellarrays, TypeScriptusuallyinferstypescorrectly, butyoumayneedtoaddtypeannotations in some cases:
430
430
431
431
```typescript
432
-
// ✅ CORRECT - Type as OpaqueRef for bidirectional binding
433
-
{items.map((item: OpaqueRef<ShoppingItem>) => (
432
+
// ✅ USUALLY WORKS - Type inference handles most cases
433
+
{items.map((item) => (
434
434
<div>
435
435
<ct-checkbox $checked={item.done}>
436
436
<span>{item.title}</span>
@@ -439,13 +439,22 @@ logic.
439
439
</div>
440
440
))}
441
441
442
-
// ❌ INCORRECT - Missing type leads to type errors with $-props
443
-
{items.map((item) => (
444
-
<ct-checkbox $checked={item.done} /> // Type error!
442
+
// ✅ ADD TYPE IF NEEDED - For complex scenarios or type errors
0 commit comments