Patching Related State
Part of the Routing and State section of Coddy's Vue journey. Lesson 37 of 55.
A patch groups related state changes into one operation. An object patch supplies the fields being changed while retaining other state. Validate input first so an invalid form submission does not update only part of a related state pair.
function chooseBundle(quantity){
if(!Number.isInteger(quantity)||quantity<1)return;
basket.$patch({quantity,package:quantity>4?'crate':'bag'});
}A function form of $patch is also useful for grouped array or object mutations, such as pushing a line and updating a revision together. Patching is about organizing changes; getters should still derive results from the resulting source state.
Challenge
MediumCommit a room plan with one $patch object. Keep input#draft local until #apply. Accept integer seat counts from 1 through 6 inclusive. Set seats to the accepted number and room to Small for at most 2, otherwise Large. Invalid input must leave both fields unchanged. Show room: seats in p#label.
Keep the supplied route readout and bootstrap files.
Try it yourself
<!doctype html><html><head><meta charset="UTF-8"><title>Routing practice</title></head><body><div id="app"></div><script type="module" src="./main.js"></script></body></html>This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Routing and State
4Query State
Reading Query ValuesCommitting Search QueriesPreserving Other FiltersNormalizing Query NumbersRecap: Catalog Filters2Navigation Actions
Navigation from HandlersReplacing HistoryHistory TravelCurrent Navigation LinksRecap: Save and Review5Nested Layouts
Parent and Child OutletsDefault Child ViewsRelative Child PathsAncestor Navigation StateRecap: Library Workspace8Store Design
Setup Store StructurePatching Related StateResetting Store StateComposing Related StoresRecap: Trip Pack11Workshop Planner Project
Planner RoutesShared Reservations