TryJS
Vue 3.5.44 · browser ESMVuenpmReactivityUI

Vue 3 Reactivity Playground — ref and computed Example

Try refs and computed values in a small Vue interface.

Loading playground…

The live editor loads here with JavaScript enabled.

Run in Playground ↗

The original source and explanation are available below.

Changes here stay separate from your main playground. Use Open in TryJS inside the editor to keep working with your changes.

Useful? Help keep TryJS free and independent.

Support TryJS

What to expect

The counter and its computed double update together. Editing the multiplier changes the derived value.

About this example

Vue can power a small browser interface without a local project. This example uses its render function API, refs and computed values. It loads an explicit Vue version from npm and does not require a single-file component compiler.

How It Works

Store a reactive value

ref stores the count. Event handlers change count.value and Vue rerenders the parts that read it.

Derive a value

computed tracks count and recalculates the derived value when it changes. h creates the preview elements directly.

Read the original source

This is the starting code. Your edits stay in the live editor above.

HTML

<div id="demo"></div>

CSS

* { box-sizing: border-box; }
body { margin: 0; padding: 24px; background: #faf8f0; color: #222; font: 15px/1.6 system-ui, sans-serif; }
h1 { font-size: 24px; line-height: 1.2; margin: 0 0 12px; }
p { margin: 8px 0 16px; }
button, input { font: inherit; }
button { padding: 9px 16px; border: 1px solid #b9a200; border-radius: 7px; background: #f0d000; color: #222; cursor: pointer; }
button:disabled { opacity: .6; cursor: wait; }
input { max-width: 100%; padding: 9px 12px; border: 1px solid #aaa; border-radius: 7px; }
small { color: #595959; }
output { font-variant-numeric: tabular-nums; }

JavaScript

import { createApp, h, ref, computed } from "vue@3.5.44";

createApp({
  setup() {
    const count = ref(0);
    const doubled = computed(() => count.value * 2);
    return () => h("main", [
      h("h1", "Vue reactivity"),
      h("p", "Count: " + count.value + " · Double: " + doubled.value),
      h("button", { onClick: () => count.value++ }, "Add one"),
    ]);
  },
}).mount("#demo");