Question presented to candidate:
"Explain what markRaw and toRaw do in Vue 3. How does each one work internally, and what can go wrong with object identity when proxies and raw objects mix?"
What a strong answer should cover:
- Vue's
reactive()returns a Proxy, a different object from the original; every nested object read through it is proxied too. markRaw(obj)sets a hidden, non-enumerable__v_skipflag, soreactive(),ref()and nested access return the object unproxied, forever.markRawis shallow: objects nested inside a marked object can still become reactive if reached another way.toRaw(proxy)returns the original object behind a reactive, readonly or shallow proxy (recursively); writes to it are not tracked.- Identity hazards:
proxy !== raw, soMap/WeakMaplookups and===checks can fail;structuredClone(proxy)throwsDataCloneError; classes with#privatefields break behind a proxy. - Both are escape hatches: use them at boundaries with non-Vue code, not as everyday tools.
Clarifying questions expected:
- "Is the object coming from a third-party library, or is it our own state?"
- "Do we need the raw object for reading, or are we going to write to it?"
Code / implementation expected: Optional β a class instance that breaks when proxied and works with markRaw, plus toRaw used for a snapshot.