dimah-surveyv0.5.0
React

Fill with React

Load a response into a SurveyJS model and connect save and submit.

useSurveyResponse() loads a response, builds a SurveyJS Model from the stored survey, restores the answers, and connects partial save and submit. Render that model with survey-react-ui.

npm i @dimah-survey/react survey-core survey-react-ui
fill.tsx
import { Survey } from "survey-react-ui";
import { createFillClient, useSurveyResponse } from "@dimah-survey/react";

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

const client = createFillClient({ baseURL: "/api/survey" });

export function Fill({ responseId }: { responseId: string }) {
  const { model, error, saveError, stale, reload } = useSurveyResponse({
    client,
    responseId,
  });

  if (error) return <p>{error.message}</p>;
  if (!model) return null;

  return (
    <>
      {saveError ? <p>{saveError.message}</p> : null}
      {stale ? <button onClick={reload}>Reload</button> : null}
      <Survey model={model} />
    </>
  );
}

Submitted and abandoned responses open in display mode and send no writes.

Options

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

Prop

Type

Handle state

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

Prop

Type

Completion waits for the server. If submit fails, SurveyJS stays open and the mounted model is preserved.

Each partial save sends the complete survey.data object; it is not a key-level patch.

Writes are queued. Each one sends expectedUpdatedAt from the last successful read. See Responses.


Bind an existing model

Use bindSurveyModel() when your application already owns the Model. useSurveyResponse() is the load plus that binding.

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

let updatedAt = initialUpdatedAt;

const dispose = bindSurveyModel(model, {
  savePartial: async (data) => {
    const saved = await client.savePartial({
      id,
      data,
      expectedUpdatedAt: updatedAt,
    });
    updatedAt = saved.updatedAt;
  },
  submit: async (data) => {
    const saved = await client.submitResponse({
      id,
      data,
      expectedUpdatedAt: updatedAt,
    });
    updatedAt = saved.updatedAt;
  },
  onWriteError: (error) => {
    console.error(error);
  },
});

Initialize the token from the response used to create model and replace it after every successful write.

import type {
  SurveyModelActions,
  SurveyModelBindOptions,
} from "@dimah-survey/react";

Prop

Type

Prop

Type

Call dispose on unmount. It removes the SurveyJS handlers it added.

bindSurveyModel also sets storeDataAsText to false on file and signature questions, including questions added later.


Keep file storage in your app

File bytes stay in your application. There is no upload route.

Handle onUploadFiles, onDownloadFile, and onClearFiles on the Model. Give SurveyJS { file, content }, where content is your URL. The binding stores that locator in data, never the bytes.

Creator autosave uses a separate binding described in Survey Creator.

On this page