Menu
CoddyTech

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 icon

Challenge

Medium

Commit 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>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Routing and State