Props flow from a parent into a child. Vue prevents replacing a prop binding inside the child, but a nested object can still be mutated through its reference unless the application enforces a stronger ownership rule. That hidden mutation makes optimistic edits, rollback, and server reconciliation difficult to reason about. A child should emit a proposed command or value change; the owning parent decides whether and when source state changes. Component keys also define identity during list updates. A row index is unstable under sorting and insertion, and a detail editor that stays mounted for a different case may keep a private draft from the old one. Decide which state belongs to the case, which belongs to the user's session, and which can be discarded.
Vue Props, Emits, and Keyed Editor Ownership
Working case
A permit card receives a case object as a prop. Its button directly changes case.status to approved before the server answers. When the server rejects, another part of the dashboard still sees the mutated object, but no rollback record exists. A queue sort then inserts case 29 ahead of case 47; index keys reuse editor instances and a private draft appears under the wrong case. The revised card emits an approval request containing the case ID. The parent owns pending, success, and rejection state. Rows use immutable case IDs as keys, and the selected editor key changes with the case ID. If the product must preserve unsent notes, the parent stores them by case ID before the editor remounts.
Implementation boundary
import { defineComponent, h } from "vue";
export const PermitCard = defineComponent({
props: { permitCase: { type: Object, required: true } },
emits: ["approve"],
setup(props, { emit }) {
return () => h("button", {
type: "button",
onClick: () => emit("approve", { caseId: props.permitCase.id })
}, `Approve ${props.permitCase.title}`);
}
});
// Parent template: <PermitCard v-for="permitCase in cases"
// :key="permitCase.id" :permit-case="permitCase"
// @approve="handleApprovalRequest" />
// Editor template: <CaseNoteEditor :key="selectedCaseId"
// :case-id="selectedCaseId" />Declare the child's props and emitted events as part of its public contract. Treat object props as read-only throughout the nested graph even if JavaScript permits mutation. Emit a command with the stable case ID and let the parent create an operation identity, apply a pending state, call the server, and reconcile the authoritative response. For a two-way editable field, use an explicit value/update event contract rather than mutating an object prop. Key queue rows by immutable case ID. Key the smallest editor subtree by selected case ID when switching should reset its local form state. Keep a separate draft map if notes must survive switching, and erase it on submission, account change, or expiry. Test both row reorder and detail switch. Do not generate a fresh random key on every render, which remounts the child unnecessarily.
Cost and boundaries
Events make ownership clear but add a parent handler and require an error result path. A pending operation map uses O(p) memory for p active mutations; saved drafts use O(d) for d retained case drafts. Keying a subtree on case change remounts its descendants and can reset focus, subscriptions, and expensive widgets. Keeping every editor alive to avoid remounts retains far more resources. The smallest keyed boundary usually balances the two, but measure it under real form complexity. Immutable case replacement may copy an array or map path, yet that cost buys a reliable before/after state for rollback and tests. Never accept a local visual approval as evidence of a committed server operation.
Failure trace
Reject an approval and verify every display returns to the server-owned status. Insert and sort rows while case 47's note is open; the note must not move to case 62. Switch to case 81 and verify the case 47 note is either stored under 47 or discarded by the declared rule. Change an unrelated toolbar label and check that the editor does not reset. Sign out and ensure private draft state is cleared. Fire two approval commands rapidly and inspect the parent's pending gate or idempotency identity. Remove a row while its request is in flight and reject a late result targeted at a different account.
Verification
- A child cannot silently commit a nested prop mutation.
- Rows and drafts remain tied to case IDs after reordering.
- A server rejection restores one authoritative status.
Practice drill
Create a parent queue with case IDs 29, 47, 62, and 81. Make a card child display status and emit an approval command without mutating the case object. Put a private note in the selected editor. Insert case 17, reverse sort, switch selection, and trigger a failed approval. Record the owner of every draft and pending operation after each step. Measure whether keying the entire queue or just the editor causes more remount work. State the draft retention period and the logout cleanup rule.
Decision note
Children propose changes; the record owner commits them, and keys follow stable entity identity.
Common Mistakes
- Mutating a nested object prop because Vue allows the JavaScript assignment.
- Using array positions as keys in a reorderable queue.
- Keying the whole page when only the case editor should reset.
Related lessons
Vue Reactivity and Component Boundaries; Vue Source State, Computed Views, and Large Snapshots; Vue Watch Cleanup and Stale Request Ownership; Vue Server Rendering and Hydration Stability; Vue Source State, Computed Views, and Large Snapshots; Autosave, Draft Recovery, and Conflicts.
Apply and check
Build Project: Vue permit review queue and review Web Development: Vue reactivity and boundaries quiz.
