[{"data":1,"prerenderedAt":325},["ShallowReactive",2],{"content-\u002Fdocs\u002Fwriting-and-mutating\u002Fvariant-memory":3},{"id":4,"title":5,"body":6,"description":303,"extension":304,"meta":305,"metaRows":306,"navigation":319,"path":320,"seo":321,"source":322,"stem":323,"__hash__":324},"docs\u002Fdocs\u002Fwriting-and-mutating\u002Fvariant-memory.md","Variant memory",{"type":7,"value":8,"toc":293},"minimark",[9,13,20,23,35,39,44,85,88,92,111,117,120,143,147,150,176,183,187,190,202,205,209,216,219,230,251,255,289],[10,11,5],"h1",{"id":12},"variant-memory",[14,15,16],"blockquote",{},[17,18,19],"p",{},"Switching away from a discriminated-union variant doesn't have to mean losing what was typed. Memory snapshots ride alongside the active state and rehydrate on switch-back. Opt out when the variants are unrelated or memory matters.",[21,22],"docs-meta-table",{},[17,24,25,26,30,31,34],{},"Two side-by-side forms with the same payment schema. Type into the card variant, switch to bank, switch back. The left form (",[27,28,29],"code",{},"rememberVariants: true",") restores your card details; the right form (",[27,32,33],{},"rememberVariants: false",") starts fresh every switch. Same schema, opposite memory policy.",[36,37],"docs-demo",{"label":38,"slug":12},"Variant Memory Demo",[40,41,43],"h2",{"id":42},"the-default","The default",[45,46,51],"pre",{"className":47,"code":48,"language":49,"meta":50,"style":50},"language-ts shiki shiki-themes github-light github-dark","useForm({\n  schema,\n  \u002F\u002F rememberVariants: true is Attaform's default\n})\n","ts","",[27,52,53,66,72,79],{"__ignoreMap":50},[54,55,58,62],"span",{"class":56,"line":57},"line",1,[54,59,61],{"class":60},"sScJk","useForm",[54,63,65],{"class":64},"sVt8B","({\n",[54,67,69],{"class":56,"line":68},2,[54,70,71],{"class":64},"  schema,\n",[54,73,75],{"class":56,"line":74},3,[54,76,78],{"class":77},"sJ8bj","  \u002F\u002F rememberVariants: true is Attaform's default\n",[54,80,82],{"class":56,"line":81},4,[54,83,84],{"class":64},"})\n",[17,86,87],{},"Switching back to a previously-visited variant lands on its prior subtree, including nested fields. Each discriminated union at every nesting depth is independently memorized: a top-level union and a nested one each keep their own memory map.",[40,89,91],{"id":90},"opting-out","Opting out",[45,93,95],{"className":47,"code":94,"language":49,"meta":50,"style":50},"useForm({ schema, rememberVariants: false })\n",[27,96,97],{"__ignoreMap":50},[54,98,99,101,104,108],{"class":56,"line":57},[54,100,61],{"class":60},[54,102,103],{"class":64},"({ schema, rememberVariants: ",[54,105,107],{"class":106},"sj4cs","false",[54,109,110],{"class":64}," })\n",[17,112,113,114,116],{},"With ",[27,115,107],{},", every switch drops the outgoing variant's typed state. The new variant initializes from its slim default; the old data is gone.",[17,118,119],{},"Use the opt-out when:",[121,122,123,131,137],"ul",{},[124,125,126,130],"li",{},[127,128,129],"strong",{},"The variants represent unrelated data."," A \"contact preference\" picker between phone and email should clear the phone when switching to email.",[124,132,133,136],{},[127,134,135],{},"Memory leaks user input you don't want re-applied."," A wizard step that should reset when the user backtracks.",[124,138,139,142],{},[127,140,141],{},"Memory-constrained targets."," Snapshots are small per-union, but a deeply nested form with many unions accumulates.",[40,144,146],{"id":145},"per-app-default","Per-app default",[17,148,149],{},"Set the default app-wide via the plugin:",[45,151,153],{"className":47,"code":152,"language":49,"meta":50,"style":50},"createAttaform({\n  defaults: { rememberVariants: false },\n})\n",[27,154,155,162,172],{"__ignoreMap":50},[54,156,157,160],{"class":56,"line":57},[54,158,159],{"class":60},"createAttaform",[54,161,65],{"class":64},[54,163,164,167,169],{"class":56,"line":68},[54,165,166],{"class":64},"  defaults: { rememberVariants: ",[54,168,107],{"class":106},[54,170,171],{"class":64}," },\n",[54,173,174],{"class":56,"line":74},[54,175,84],{"class":64},[17,177,178,179,182],{},"Per-form ",[27,180,181],{},"useForm({ rememberVariants: true })"," overrides back to memory-on for forms that want it.",[40,184,186],{"id":185},"what-gets-memorized","What gets memorized",[17,188,189],{},"When the discriminator value flips:",[191,192,193,196,199],"ol",{},[124,194,195],{},"The current variant's subtree is snapshotted into the memory map keyed by the outgoing variant's discriminator value.",[124,197,198],{},"The new variant's slim default seeds storage.",[124,200,201],{},"On a subsequent switch to a remembered variant, the snapshot rehydrates over the seeded default.",[17,203,204],{},"What's stored: the value subtree only. Field-state (touched, blurred, focused, etc.) is NOT part of the snapshot; that travels with the active field state, which lives outside the union's storage tree.",[40,206,208],{"id":207},"memory-is-in-memory-only","Memory is in-memory only",[17,210,211,212,215],{},"Variant memory does NOT survive a page reload. Persisted state (",[27,213,214],{},"useForm({ persist: 'local' })",") restores values into form storage on hydration, but the variant memory map starts empty: the first discriminator switch after reload loses any persisted typing in the outgoing variant.",[17,217,218],{},"For cross-session continuity of inactive-variant typing, persist beyond the union boundary yourself. Mirror the inactive subtree into a separate persisted slot via a watcher on the discriminator.",[40,220,222,225,226,229],{"id":221},"reset-and-resetfield-interactions",[27,223,224],{},"reset()"," and ",[27,227,228],{},"resetField()"," interactions",[121,231,232,239],{},[124,233,234,238],{},[127,235,236],{},[27,237,224],{}," clears all variant memory. The reset state becomes the new \"no memory\" baseline.",[124,240,241,246,247,250],{},[127,242,243],{},[27,244,245],{},"resetField(path)"," clears any memory entry whose union path equals or sits under ",[27,248,249],{},"path",". Resetting a single union's path drops only that union's memory; sibling unions retain theirs.",[40,252,254],{"id":253},"where-to-next","Where to next",[121,256,257,265,278],{},[124,258,259,264],{},[260,261,263],"a",{"href":262},"\u002Fdocs\u002Fschemas\u002Fdiscriminated-unions","Discriminated unions",": the schema feature variant memory rides on top of.",[124,266,267,277],{},[260,268,270,273,274],{"href":269},"\u002Fdocs\u002Fwriting-and-mutating\u002Freset",[27,271,272],{},"reset"," & ",[27,275,276],{},"resetField",": both interact with the memory map deterministically.",[124,279,280,284,285,288],{},[260,281,283],{"href":282},"\u002Fdocs\u002Fcross-cutting-state\u002Fapp-defaults","App-wide defaults",": set ",[27,286,287],{},"rememberVariants"," once for every form in the app.",[290,291,292],"style",{},"html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":50,"searchDepth":68,"depth":68,"links":294},[295,296,297,298,299,300,302],{"id":42,"depth":68,"text":43},{"id":90,"depth":68,"text":91},{"id":145,"depth":68,"text":146},{"id":185,"depth":68,"text":186},{"id":207,"depth":68,"text":208},{"id":221,"depth":68,"text":301},"reset() and resetField() interactions",{"id":253,"depth":68,"text":254},"rememberVariants keeps the inactive variant's typed subtree alive so switching back restores it. Opt out for wizards that should reset on backtrack, or for memory-constrained targets.","md",{},[307,310,313,316],{"label":308,"value":309},"Category","Form option",{"label":311,"value":312,"kind":27},"Option","useForm({ rememberVariants })",{"label":314,"value":315,"kind":27},"Default","true",{"label":317,"value":318},"Lifetime","in-memory only (no persistence)",true,"\u002Fdocs\u002Fwriting-and-mutating\u002Fvariant-memory",{"title":5,"description":303},null,"docs\u002Fwriting-and-mutating\u002Fvariant-memory","Wqf8ZyJtbHQRB00jShwKJuM_-2qJcX2zuCDQx0teLbE",1780949760038]