React

Build powerful multi-step forms and surveys in your React app.

Forms.md is a pure JavaScript library with minimal dependencies. All it requires is an input (form template) as a plain string, and a container to render the form in. This means that it is very easy to use with React, Angular, Vue, Svelte, etc. The only "catch" is that the forms only work on the client side, that is, where window and document are available.


Use with React

Given below is an example of a form working inside a React app:

MailingListForm.tsx

In the component below, the form template is created using the Composer. The actual component only returns an empty <div> element. Once the component has mounted, the form is then initialized and rendered.

// "use client";

import "formsmd/dist/css/formsmd.min.css";
import { useEffect, useRef } from "react";
import { Composer, Formsmd } from "formsmd";

const composer = new Composer({
  id: "mailing-list-form",
  postUrl: "/api/mailing-list",
});

composer.emailInput("email", {
  question: "Join our mailing list",
  description:
    "Stay informed of every update that matters - we'll deliver the latest news straight to your inbox.",
  required: true,
});

export default function MailingListForm() {
  const containerRef = useRef(null);

  useEffect(() => {
    if (containerRef.current) {
      const formsmd = new Formsmd(composer.template, containerRef.current, {
        postHeaders: {
          Authorization: `Basic ${process.env.PUBLIC_API_KEY}`,
        },
      });
      formsmd.init();
    }
  }, []);

  return (
    <div ref={containerRef} style={{ width: "500px", height: "500px" }}></div>
  );
}

page.tsx

import MailingListForm from "./MailingListForm";

export default function Home() {
  return (
    <MailingListForm />
  );
}

Next.js, Remix, Astro, etc., and SSR

As mentioned above, the forms only work on the client-side. If you're using a framework which uses SSR, you'll need to make sure the components that use Forms.md are rendered only on the client-side. How you do this will vary from framework to framework. For example, in Next.js, this can be done with the "use client" directive.

Last updated

Was this helpful?