dimah-surveyv0.5.0
React

Survey Creator

Bind Creator autosave to the editor draft while keeping publish explicit.

useSurveyDraft() connects a Creator instance to saveSurvey. Autosave replaces draftJson; it never changes publishedJson or any response definition.

npm i @dimah-survey/react survey-core survey-react-ui survey-creator-core survey-creator-react

Publishing is an explicit product action, not an autosave side effect. New responses see the draft only after publishSurvey.

components/design.tsx
"use client";

import { useEffect, useState } from "react";
import dynamic from "next/dynamic";
import type { SurveyJson } from "@dimah-survey/core";
import { createEditorClient, useSurveyDraft } from "@dimah-survey/react";
import type { SurveyCreator } from "survey-creator-react";

import "survey-core/survey-core.min.css";
import "survey-creator-core/survey-creator-core.min.css";

const CreatorSurface = dynamic(
  () =>
    import("survey-creator-react").then((mod) => mod.SurveyCreatorComponent),
  { ssr: false },
);

const editorClient = createEditorClient({ baseURL: "/api/admin/survey" });

export function Design({
  surveyId,
  draftJson,
  updatedAt,
}: {
  surveyId: string;
  draftJson: SurveyJson;
  updatedAt: string;
}) {
  const snapshot = JSON.stringify(draftJson);
  const [creator, setCreator] = useState<SurveyCreator | null>(null);

  const { saveError, stale } = useSurveyDraft({
    client: editorClient,
    surveyId,
    creator,
    updatedAt,
  });

  useEffect(() => {
    let disposed = false;
    let instance: SurveyCreator | undefined;

    void import("survey-creator-react").then(({ SurveyCreator }) => {
      if (disposed) return;
      instance = new SurveyCreator();
      instance.JSON = JSON.parse(snapshot) as SurveyJson;
      setCreator(instance);
    });

    return () => {
      disposed = true;
      instance?.dispose();
    };
  }, [snapshot, surveyId]);

  if (!creator) return <p>Loading…</p>;

  return (
    <>
      {saveError ? <p>{saveError.message}</p> : null}
      {stale ? <p>This draft was saved somewhere else.</p> : null}
      <CreatorSurface creator={creator} />
    </>
  );
}

Load draftJson and updatedAt on the server with editor.api.getSurvey, then pass both into the client component. createEditorClient() defaults to /api/admin/survey. Creator is browser-only, so Next.js loads both the class and component dynamically.

import type { UseSurveyDraftOptions } from "@dimah-survey/react";

Prop

Type

The hook sets isAutoSave and saveSurveyFunc. Each save sends expectedUpdatedAt from the loaded survey, then from the last successful write. A new updatedAt prop rebinds that token.

import type { SurveyDraftBinding } from "@dimah-survey/react";

Prop

Type

Unlike the fill hook, useSurveyDraft() has no reload() method. Reload the survey in your application before writing again.


Publish explicitly

Autosave must not call publishSurvey. Publish copies the current draft onto publishedJson. Wait for the current Creator save, then publish the revision you just read:

import type { SurveyCreator } from "survey-creator-react";

function flushCreatorSave(creator: SurveyCreator) {
  return new Promise<void>((resolve, reject) => {
    creator.saveSurveyFunc(0, (_saveNo, success) => {
      if (success) resolve();
      else reject(new Error("Could not save the draft."));
    });
  });
}

await flushCreatorSave(creator);
const latest = await editorClient.getSurvey(surveyId);
await editorClient.publishSurvey({
  id: surveyId,
  expectedUpdatedAt: latest.updatedAt,
});

On the server, the equivalent call is editor.api.publishSurvey({ body }). Archive, resume, and settings are editor calls too. See the complete Next.js example.


Bind without React state

bindSurveyCreator() exposes the same binding without React state. Pass initialUpdatedAt from the load that produced the Creator instance. The app still constructs, renders, and disposes Creator.

import type { SurveyCreatorActions } from "@dimah-survey/react";

Prop

Type

survey-creator-core and survey-creator-react stay in your application. They are not dependencies of @dimah-survey/react.

See Surveys for publish, archive, resume, and concurrent edits.

On this page