Show without Unmounting
Part of the Fundamentals section of Coddy's Vue journey. Lesson 22 of 55.
Use v-show when hiding should keep an element mounted. This panel starts hidden, but its input still exists in the DOM.
const expanded = ref(false);
// <div v-show="expanded"><input aria-label="Memo"></div>The directive toggles CSS display. Showing the panel again reuses the existing input, including text the user typed. Unlike v-if, it does not create an exclusive branch chain with v-else. Choose according to whether the hidden content should stay mounted.
Challenge
EasyKeep a note panel div#result mounted but initially hidden using v-show. Button #toggle switches visibility. Inside it keep an ordinary unbound input#draft. Text typed there must survive hiding and showing the panel.
Try it yourself
<!doctype html><html><head><meta charset="UTF-8"><title>Vue 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 Fundamentals
2Reactive State
Reactive RefsUpdating Refs in ScriptEvent Handler ArgumentsReactive ObjectsRecap: Reactive State5Conditions and Lists
Conditional BranchesShow without UnmountingRendering ListsStable List IdentityRecap: Conditions8Component Communication
Emitting EventsEvent PayloadsLifting Shared StateDefault and Named SlotsRecap: Shared Panel11Project: Community Board
Board StructureAdding Community Tasks