🍡 mochi
← All demos

Using form return data

An action returns a payload via success(...). With the enhance attachment, the JSON result updates the UI in place. Without it, the page re-renders and the component reads the value from the request's form snapshot.

A minimal action that returns a random number via success({ value }). The hydrated version updates the input reactively; the non-hydrated version re-renders the whole page and the component reads the value from getRequestContext().form.

With {@attach enhance(...)}

The enhance attachment fills the input via fetch + JSON, no reload

Plain HTML

Full HTML POST, page re-renders, RandomRoll reads the value from the request's form snapshot via !isHydratable && isServer

<script lang="ts">
  import RandomRoll from './RandomRoll.svelte';
</script>

<p>
  A minimal action that returns a random number via <code>success({'{ value }'})</code>. The hydrated version updates the input reactively; the non-hydrated version re-renders
  the whole page and the component reads the value from <code>getRequestContext().form</code>.
</p>
<h3>With <code>{'{@attach enhance(...)}'}</code></h3>
<RandomRoll label="The enhance attachment fills the input via fetch + JSON, no reload" mochi:hydrate />
<h3>Plain HTML</h3>
<RandomRoll label="Full HTML POST, page re-renders, RandomRoll reads the value from the request's form snapshot via !isHydratable && isServer" />
Styles
<style>
  h3 {
    margin: 1.5rem 0 0.25rem;
    font-size: 0.95rem;
    font-weight: 600;
    color: var(--text-muted);
  }

  p {
    margin: 0 0 0.5rem;
    font-size: 0.9rem;
    color: var(--text-muted);
  }

  code {
    background: var(--code-bg);
    color: var(--code-accent);
    padding: 0.05rem 0.35rem;
    border-radius: var(--radius-sm);
    font-family: var(--font-mono);
    font-size: 0.85rem;
  }
</style>