SurveyJS React UI

2.5.20 · active · verified Wed Apr 22

survey-react-ui is a free, MIT-licensed React UI component library designed to render dynamic, interactive JSON-based forms and surveys directly within React applications. It enables developers to easily design and deploy complex, data-driven forms, polls, and quizzes, collecting user responses. The package is currently at version 2.5.20 and follows a rapid release cadence, with frequent minor and patch updates as indicated by the successive 2.5.x versions. Key differentiators include a rich set of over 20 built-in question types, support for custom question types, integrated themes with extensive CSS customization options, robust answer validation, and comprehensive TypeScript support. It is built upon `survey-core` for its underlying survey model logic and is broadly compatible with React versions 16.5, 17, 18, and 19. The library is backend-agnostic, with provided examples for Node.js, PHP, and ASP.NET environments.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a SurveyJS `Model` from a JSON definition and render it using the `Survey` React component. It includes basic state management for displaying results after completion and highlights the necessary CSS import for styling.

import React, { useState, useEffect } from 'react';
import { Survey } from 'survey-react-ui';
import { Model } from 'survey-core';
import 'survey-core/defaultV2.min.css'; // Or 'survey-core/modern.min.css'

const surveyJson = {
  title: 'Sample Survey',
  description: 'Please answer the following questions.',
  pages: [
    {
      name: 'page1',
      elements: [
        {
          type: 'text',
          name: 'name',
          title: 'What is your name?',
          isRequired: true
        },
        {
          type: 'dropdown',
          name: 'country',
          title: 'Which country do you live in?',
          choices: ['USA', 'Canada', 'Mexico', 'Germany', 'France'],
          isRequired: true
        }
      ]
    },
    {
      name: 'page2',
      elements: [
        {
          type: 'comment',
          name: 'feedback',
          title: 'Any additional feedback?'
        }
      ]
    }
  ]
};

export default function MySurveyComponent() {
  const [surveyModel, setSurveyModel] = useState(null);
  const [surveyResults, setSurveyResults] = useState(null);

  useEffect(() => {
    const model = new Model(surveyJson);
    model.onComplete.add((sender, options) => {
      console.log('Survey results:', sender.data);
      setSurveyResults(sender.data);
      // In a real app, you would send sender.data to your backend
    });
    setSurveyModel(model);
  }, []);

  if (!surveyModel) {
    return <div>Loading survey...</div>;
  }

  return (
    <div>
      {surveyResults ? (
        <div>
          <h2>Thank you for completing the survey!</h2>
          <pre>{JSON.stringify(surveyResults, null, 2)}</pre>
        </div>
      ) : (
        <Survey model={surveyModel} />
      )}
    </div>
  );
}

view raw JSON →