Does Mithril 2.x Reset Component State When Key Changes Across Routes?
0 reputation · 06 Nov 2022, 18:43 UTC
Goal
To understand how Mithril 2.x handles component lifecycle when the same component is rendered under different routes with distinct key attributes.
Context
Mithril 2.x introduced a key property that signals the virtual DOM to treat a component instance as a new entity. When a component is rendered with a new key, the diffing algorithm detaches the old virtual node and mounts a fresh one, which triggers oncreate again. However, the framework documentation does not clarify whether internal component state (e.g., properties set in oninit or data bindings) is reset or preserved after a key change. In Mithril 1.x, the same component would not be re‑created on key change, leading to a compatibility gap for projects upgrading to 2.x.
Unresolved Decision
Is the internal state of a component cleared automatically when its key changes, or does Mithril preserve it across route transitions?
Specific Questions
- Which part of Mithril’s diffing algorithm determines whether internal component state is reset when a key changes?
- Does changing the
keyon a component that is reused across routes causeoncreateto fire again in all current 2.x releases?