Add Vue Reactivity Best Practices section to CLAUDE.md
- Introduced guidelines for handling complex object reactivity in Vue, addressing common issues such as input components not updating and computed properties not recalculating. - Provided solutions including using Object.assign for object reactivity, dynamic keys for component re-renders, and safe navigation with fallbacks. - Included practical examples to illustrate the application of these patterns in real scenarios, enhancing developer understanding of Vue's reactivity system.
This commit is contained in:
parent
0bab2ec444
commit
7b762a1e4b
1 changed files with 69 additions and 0 deletions
69
CLAUDE.md
69
CLAUDE.md
|
|
@ -370,6 +370,75 @@ const onSubmit = form.handleSubmit(async (values) => {
|
|||
<Button :disabled="!isFormValid">Submit</Button>
|
||||
```
|
||||
|
||||
### **Vue Reactivity Best Practices**
|
||||
|
||||
**CRITICAL: Handling Complex Object Reactivity**
|
||||
|
||||
When working with complex objects from API responses or services, Vue's reactivity system may not always detect changes properly. This is especially common with nested objects or objects from external sources.
|
||||
|
||||
**Common Reactivity Issues:**
|
||||
- Input components not updating when object properties change
|
||||
- Template not re-rendering after API responses
|
||||
- Computed properties not recalculating
|
||||
|
||||
**✅ SOLUTIONS:**
|
||||
|
||||
**1. Force Object Reactivity with Object.assign:**
|
||||
```typescript
|
||||
// ❌ DON'T: Direct assignment may not trigger reactivity
|
||||
createdObject.value = apiResponse
|
||||
|
||||
// ✅ DO: Create new object reference to ensure reactivity
|
||||
createdObject.value = Object.assign({}, apiResponse)
|
||||
```
|
||||
|
||||
**2. Force Component Re-render with Dynamic Keys:**
|
||||
```vue
|
||||
<!-- ❌ DON'T: Component may not update with new data -->
|
||||
<Input :value="object.property" readonly />
|
||||
|
||||
<!-- ✅ DO: Force re-render with dynamic key -->
|
||||
<Input
|
||||
:key="`field-${object?.id}`"
|
||||
:model-value="object?.property || ''"
|
||||
readonly
|
||||
/>
|
||||
```
|
||||
|
||||
**3. Use Safe Navigation and Fallbacks:**
|
||||
```vue
|
||||
<!-- ✅ Prevent errors and ensure consistent data types -->
|
||||
<Input
|
||||
:model-value="object?.property || ''"
|
||||
:key="`field-${object?.uniqueId}`"
|
||||
readonly
|
||||
/>
|
||||
```
|
||||
|
||||
**When to Apply These Patterns:**
|
||||
- ✅ API responses stored in reactive refs
|
||||
- ✅ Complex objects from services
|
||||
- ✅ Input components showing external data
|
||||
- ✅ Any scenario where template doesn't update after data changes
|
||||
|
||||
**Example from Wallet Module:**
|
||||
```typescript
|
||||
// Service returns complex invoice object
|
||||
const invoice = await walletService.createInvoice(data)
|
||||
|
||||
// Force reactivity for template updates
|
||||
createdInvoice.value = Object.assign({}, invoice)
|
||||
```
|
||||
|
||||
```vue
|
||||
<!-- Template with forced reactivity -->
|
||||
<Input
|
||||
:key="`bolt11-${createdInvoice?.payment_hash}`"
|
||||
:model-value="createdInvoice?.payment_request || ''"
|
||||
readonly
|
||||
/>
|
||||
```
|
||||
|
||||
### **Module Development Best Practices**
|
||||
|
||||
**Module Structure Requirements:**
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue