Skip to content
Gaurav TiwariDocs

React, Vue and Svelte

Version 5.2.5 Updated November 22, 2026

Single-page apps often add a form’s element after the page has loaded. These components load the Core Forms embed once and mount each form when its element is ready.

They use the drop-in embed, so the API key ships to the browser. If your app has a backend, use a server route instead.

Add the Mount Helper

Finish the WordPress setup, save this as mount-core-form.js next to your components and change the URL:

const EMBED_URL = 'https://cms.example.com/wp-json/core-forms/v1/embed.js';

export function mountCoreForm(host) {
  if (!host || host.dataset.cfRequested) return;
  host.dataset.cfRequested = '1';

  if (window.CoreFormsEmbed) {
    window.CoreFormsEmbed.mount(host);
    return;
  }

  // On first load, embed.js mounts every host that is already in the page.
  if (!document.querySelector(`script[src="${EMBED_URL}"]`)) {
    const script = document.createElement('script');
    script.src = EMBED_URL;
    document.head.appendChild(script);
  }
}

The helper handles both cases. It loads the script for the first form and calls mount() for forms that appear later.

React

'use client';

import { useEffect, useRef } from 'react';
import { mountCoreForm } from './mount-core-form';

export function EmbedForm({ form, apiKey }) {
  const host = useRef(null);

  useEffect(() => {
    mountCoreForm(host.current);
  }, []);

  return <div ref={host} data-cf-form={form} data-cf-key={apiKey} />;
}

Use it with a key from your public environment. This example uses Vite:

<EmbedForm key="contact" form="contact" apiKey={import.meta.env.VITE_CF_KEY} />

Set key to the form slug so React creates a new host when the form changes.

Vue

<script setup>
import { onMounted, ref } from 'vue';
import { mountCoreForm } from './mount-core-form';

defineProps({
  form: { type: String, required: true },
  apiKey: { type: String, required: true },
});

const host = ref(null);
onMounted(() => mountCoreForm(host.value));
</script>

<template>
  <div ref="host" :data-cf-form="form" :data-cf-key="apiKey"></div>
</template>
<EmbedForm key="contact" form="contact" :api-key="publicKey" />

Define publicKey in the parent’s script, for example from import.meta.env.VITE_CF_KEY.

Svelte

<script>
  import { onMount } from 'svelte';
  import { mountCoreForm } from './mount-core-form';

  export let form;
  export let apiKey;
  let host;

  onMount(() => mountCoreForm(host));
</script>

<div bind:this={host} data-cf-form={form} data-cf-key={apiKey}></div>
<EmbedForm form="contact" apiKey={publicKey} />

If the form can change while the component stays on the page, wrap it in {#key form}.

Update the Interface

cf:success fires for every accepted submission, including forms that redirect. Listen on the document or a parent element:

function onSuccess(event) {
  console.log('Submitted', event.detail);
}

document.addEventListener('cf:success', onSuccess);

// Run this when the component unmounts.
document.removeEventListener('cf:success', onSuccess);

Don’t render children inside the host element. The embed replaces its contents.

Keep the Key Private

Anything named VITE_, PUBLIC_ or NEXT_PUBLIC_ is public. To keep the key private:

  1. Add a server route from the Next.js, Node.js, PHP or Cloudflare guide.
  2. Render the fields with your own UI.
  3. Post to your route, with no key in the browser.

The drop-in embed always posts straight to WordPress, so it can’t use a proxy route.

Share

Was this helpful?

Still stuck? Open a support ticket