Build Vue's proxy variants: readonly, shallowReactive, shallowRef with triggerRef, markRaw, toRaw and ref unwrapping

hardAbout 55 minutesUpdated CodingProvuereactivityproxyreadonlyshallow-ref
Not solved

Share this question

Problem

reactive() and ref() are the default, but real Vue apps lean on the variants: readonly for state a child must not change, shallowRef for a large list you replace rather than mutate, markRaw for a chart instance or SDK client that must never be proxied, and toRaw to hand plain data to a library. Most of them are the same Proxy with two flags. Build them on the small tracking core in the starter.

const view = readonly(reactive({ count: 1 }));
effect(() => console.log(view.count)); // 1
toRaw(view).count; // 1, the original object
view.count = 5; // ignored
const rows = shallowRef([1, 2]);
rows.value.push(3); // nothing re-runs
triggerRef(rows); // effects that read rows.value re-run

Ref. UR-1461 · © UIRound · uiround.com/questions/build-vues-proxy-variants-readonly-shallowreactive-shallowref-with-triggerref-markraw-toraw-and-ref-unwrapping

Stuck? Take a hint

Try it in the editor

Run your code as often as you like. Submitting it to the full test suite is part of Pro.

Private, and saved in this browser as you type. All your notes are on My lists.

Every question and answer7 days, one payment