Survey Website using ReactJS
Last Updated :
23 Jul, 2025
In this article, we will create a survey website template using ReactJS. This project basically creates a single page website which will take input from user in multiple states and stores it to be displayed later. The user has to enter basic details like name, email-Id and contact number. After filling all of these, only then user will be allowed to answer next questions. Information filled by user will be displayed to user for conformation, which user has to conform and submit. All the logic and backend of website is done using JSX.
Final Output of Website will look something like this:
Technologies Used/Pre-requisites
- ReactJS
- Bootstrap
- CSS
- JSX
- Export default functions in ReactJS
Approach:
In order to create a multi state survey application in react we start by creating all form components required. We create a 'basicDetails' component which will take basic details of user and store it. We will create a questionnaire next which contain survey questions. Third page will display all details entered by user, and a submit button to conform submission which redirects us to 'ThankYou' page. We will also have additional components such as 'Header', 'Footer' and 'About' sections. We will create a backend by 'useState' hook to take values for all inputs in forms, and make sure they are changed before the final 'EnteredDetails' page by implementing 'useEffect' hook. For storing temporary data we will use localStorage to store user data. Styling will be done by using Bootstrap. We will navigate in our website using 'react-router-dom' package of React.
Steps to create the application:
Step 1: Setup React Project: Open a terminal and run following command to create a new React project (enter name of the project in '< >')
npm create vite@latest <name-of-project> --template react
Step 2: Change Directory: Go to directory of project.
cd <<name of project>>
Step 3: Install router package: Install React router package, which will enable us navigating in our website.
npm install react-router-dom
Step 4: Import Bootstrap css and js import files from Bootstrap website into the index.html in public folder, which will enable us to use styles and methods of Bootstrap.
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet"
crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
crossorigin="anonymous">
</script>
Step 5: Create a folder called Components in src directory and create the files About.js, AdditionalQuestions.js, BasicInfo.js, EnteredDetails.js, Footer.js, Header.js, ThankYouPage.js
Project Structure:
The dependencies in 'package.json' should look like:
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.14.1",
"vite": "^4.1.0",
"bootstrap": "^5.0.2"
}
Example: Write the following code in respective files:
- App.js: This file imports all the components and renders it on the webpage
- App.css: This file contains the styling of the application.
CSS
/* App.css */
body{
background-color: #F2EAD3;
}
.navbar{
width : 59vw;
text-decoration: none;
height: 5vh;
top: 0%;
position: fixed;
}
.qform{
margin-top: 10vh;
margin-bottom: 5vh;
z-index: 0;
}
.badge{
color: black;
}
.footer {
background-color: green;
position: fixed;
width: 100%;
bottom: 0;
color: white;
}
JavaScript
// App.js
import './App.css';
import React, { useEffect, useState } from 'react';
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
import BasicInfo from './Components/BasicInfo';
import AdditionalQuestions from './Components/AdditionalQuestions';
import EnteredDetails from './Components/EnteredDetails';
import ThankYouPage from './Components/ThankYouPage';
import { About } from './Components/About';
function App() {
// Initialize basicData state from localStorage or an empty object
const initBasicData = JSON.parse(localStorage.getItem('data')) || {};
// Initialize questionData state from localStorage or an empty object
const initQuestionsData = JSON.parse(localStorage.getItem('questiondata')) || {};
// Set up state hooks for basicData and questionData
const [basicData, setBasicData] = useState(initBasicData);
const [questionData, setQuestionData] = useState(initQuestionsData);
// Update localStorage whenever basicData changes
useEffect(() => {
localStorage.setItem('data', JSON.stringify(basicData));
}, [basicData]);
// Update localStorage whenever questionData changes
useEffect(() => {
localStorage.setItem('questiondata', JSON.stringify(questionData));
}, [questionData]);
// Function to add basicData to state and localStorage
const addBasicData = (name, email, contact) => {
// Create an object with the provided basic data
const myBasicData = {
name: name,
email: email,
contact: contact
};
// Update the basicData state with the new data
setBasicData(myBasicData);
// Update the localStorage with the new basicData
localStorage.setItem("data", JSON.stringify(myBasicData));
}
// Function to add questionData to state and localStorage
const addQuestionData = (profession, interest, reference) => {
// Create an object with the provided question data
const myQuestionData = {
profession: profession,
interest: interest,
reference: reference
};
// Update the questionData state with the new data
setQuestionData(myQuestionData);
// Update the localStorage with the new questionData
localStorage.setItem("questiondata", JSON.stringify(myQuestionData));
}
// Render the application
return (
<Router>
{/* Define the routes */}
<Routes>
{/* Render the BasicInfo component with the addBasicData function */}
<Route path='/' element={<BasicInfo addBasicData={addBasicData} />} />
{/* Render the AdditionalQuestions component with the addQuestionData function */}
<Route
path='/questions'
element={<AdditionalQuestions addQuestionData={addQuestionData} />}
/>
{/* Render the EnteredDetails component with basicData and questionData */}
<Route
path='/details'
element={<EnteredDetails data={basicData} questiondData={questionData} />}
/>
{/* Render the ThankYouPage component */}
<Route
path='/thanks'
element={<ThankYouPage />}
/>
{/* Render the About component */}
<Route
path='/about'
element={<About />}
/>
</Routes>
</Router>
);
}
// Export the App component as the default export
export default App;
Write the following code in files created in the Components folder
- BasicInfo.js: Here the user's basic information is collected
- AdditionalQuestions.js: Here, extra information provided by user is collected
- EnteredDetails.js: Here, the details entered are shown again after all the data is submitted
- Header.js: This component displays the header of the webpage
- Footer.js: This component displays the footer of the webpage
- ThankYouPage.js: This page displays after all the information is collected
- About.js: Extra information about webpage is shown in this page.
JavaScript
// BasicInfo.js
import React, { useState } from 'react';
import { useNavigate } from 'react-router-dom';
export default function BasicInfo({ addBasicData }) {
// State variables to store user input
const [name, setName] = useState("");
const [email, setEmail] = useState("");
const [contact, setContact] = useState("");
// Navigation function for programmatic routing
const navigate = useNavigate();
// Function to handle form submission
const submit = (e) => {
e.preventDefault();
if (!name || !email || !contact) {
// Alert if any field is missing
alert("All fields necessary!");
} else {
// Call the addBasicData function provided by the parent component
addBasicData(name, email, contact);
// Navigate to the '/questions' route
navigate('/questions');
}
}
return (
<div className="container-fluid qform">
<div className="col-md-5 m-auto">
<div className="mt-3">
<div className="card text-left h-100">
<div className="card-body my-3">
<form onSubmit={submit}>
<label htmlFor="">
<h4>Basic Details</h4>
</label>
<div className="form-group my-3">
<label htmlFor="">
<b>1.</b> Name
</label>
{/* Input field for name */}
<input
type="text"
name="name"
value={name}
onChange={(e) => { setName(e.target.value) }}
className='form-control my-2'
placeholder='Enter your Name'
autoComplete='off'
/>
</div>
<div className="form-group my-3">
<label htmlFor="">
<b>2.</b> Email
</label>
{/* Input field for email */}
<input
type="email"
name='email'
value={email}
onChange={(e) => { setEmail(e.target.value) }}
className='form-control my-2'
placeholder='Enter your Email'
autoComplete='off'
/>
</div>
<div className="form-group my-3">
<label htmlFor="">
<b>3.</b> Contact No.
</label>
{/* Input field for contact number */}
<input
type="tel"
name='contact'
value={contact}
onChange={(e) => { setContact(e.target.value) }}
className='form-control my-2'
placeholder='Enter your Contact No.'
autoComplete='off'
/>
</div>
{/* Submit button */}
<button type='submit' className='btn btn-success mx-3'>Next</button>
</form>
{/* Step indicators */}
<center>
<span className="badge badge-pill bg-success"><b>1</b></span>
<span className="badge rounded-pill disabled">2</span>
<span className="badge rounded-pill disabled">3</span>
</center>
</div>
</div>
</div>
</div>
</div>
)
}
JavaScript
// AdditionalQuestions.js
import React, { useState } from 'react';
import { useNavigate } from 'react-router-dom';
export default function AdditionalQuestions({ addQuestionData }) {
// State variables to store user inputs
const [profession, setProfession] = useState("");
const [interest, setInterest] = useState("");
const [reference, setReference] = useState("");
const [otherProfession, setOtherProfession] = useState("");
const [otherInterest, setOtherInterest] = useState("");
const [otherReference, setOtherReference] = useState("");
const navigate = useNavigate();
// Function to handle form submission
const submit = (e) => {
e.preventDefault();
// Check if all fields are filled
if (!profession || !interest || !reference) {
alert("All fields necessary!");
} else {
// If the selected option is "Others", use the value from the corresponding text input
if (profession === "Others") {
profession = otherProfession;
}
if (interest === "Others") {
interest = otherInterest;
}
if (reference === "Others") {
reference = otherReference;
}
// Log the selected options and call the addQuestionData function with the data
console.log(profession, interest, reference);
addQuestionData(profession, interest, reference);
// Navigate to the next page
navigate('/details');
}
};
// Event handler for changes in the profession radio buttons
const handleProfessionChange = (e) => {
setProfession(e.target.value);
};
// Event handler for changes in the interest radio buttons
const handleInterestChange = (e) => {
setInterest(e.target.value);
};
// Event handler for changes in the reference radio buttons
const handleReferenceChange = (e) => {
setReference(e.target.value);
};
return (
<div className="container-fluid qform">
<div className="col-md-5 m-auto">
<div className="mt-3">
<div className="card text-left h-100">
<div className="card-body">
<form onSubmit={submit}>
<label htmlFor="">
<h4>Additional Questions</h4>
</label>
{/* Profession options */}
<div className="form-group m-2" onChange={handleProfessionChange}>
<label htmlFor="q1">
<b>1.</b> What is your profession?
</label>
<br />
<input
type="radio"
name="ProfessionRadio"
id="student"
autoComplete="off"
className="m-2"
value="Student"
/>
<label htmlFor="student"> Student</label>
<br />
{/* Other options for profession with text input */}
<input
type="radio"
name="ProfessionRadio"
id="sde"
autoComplete="off"
className="m-2"
value="Software Engineer"
/>
<label htmlFor="sde"> Software Engineer</label>
<br />
<input
type="radio"
name="ProfessionRadio"
id="teacher"
autoComplete="off"
className="m-2"
value="Teacher"
/>
<label htmlFor="teacher"> Teacher</label>
<br />
<input
type="radio"
name="ProfessionRadio"
id="others"
autoComplete="off"
className="m-2"
value="Others"
/>
<label htmlFor="others"> Others:</label>
<input
type="text"
id="otherProfession"
autoComplete="off"
className="form-control m-2"
value={otherProfession}
onChange={(e) => { setOtherProfession(e.target.value) }}
/>
<hr />
</div>
{/* Interest options */}
<div className="form-group m-2" onChange={handleInterestChange}>
<label htmlFor="q2">
<b>2.</b> What are your interests?
</label>
<br />
<input
type="radio"
name="interestRadio"
id="dsa"
autoComplete="off"
className="m-2"
value="DSA"
/>
<label htmlFor="dsa"> DSA</label>
<br />
{/* Other options for interest with text input */}
<input
type="radio"
name="interestRadio"
id="fullstack"
autoComplete="off"
className="m-2"
value="Full Stack Development"
/>
<label htmlFor="fullstack"> Full Stack Development</label>
<br />
<input
type="radio"
name="interestRadio"
id="dataScience"
autoComplete="off"
className="m-2"
value="Data Science"
/>
<label htmlFor="dataScience"> Data Science</label>
<br />
<input
type="radio"
name="interestRadio"
id="compeProgramming"
autoComplete="off"
className="m-2"
value="Competitive Programming"
/>
<label htmlFor="compeProgramming"> Competitive Programming</label>
<br />
<input
type="radio"
name="interestRadio"
id="others"
autoComplete="off"
className="m-2"
value="Others"
/>
<label htmlFor="others"> Others:</label>
<input
type="text"
id="otherInterest"
autoComplete="off"
className="form-control m-2"
value={otherInterest}
onChange={(e) => { setOtherInterest(e.target.value) }}
/>
<hr />
</div>
{/* Reference options */}
<div className="form-group m-2" onChange={handleReferenceChange}>
<label htmlFor="q3">
<b>3.</b> Where did you hear about us?
</label>
<br />
<input
type="radio"
name="referenceRadio"
id="news"
autoComplete="off"
className="m-2"
value="News Paper"
/>
<label htmlFor="news"> News Paper</label>
<br />
<input
type="radio"
name="referenceRadio"
id="LinkedIn"
autoComplete="off"
className="m-2"
value="LinkedIn"
/>
<label htmlFor="LinkedIn"> LinkedIn</label>
<br />
<input
type="radio"
name="referenceRadio"
id="Instagram"
autoComplete="off"
className="m-2"
value="Instagram"
/>
<label htmlFor="Instagram"> Instagram</label>
<br />
<input
type="radio"
name="referenceRadio"
id="others"
autoComplete="off"
className="m-2"
value="Others"
/>
<label htmlFor="others"> Others:</label>
<input
type="text"
id="otherReference"
autoComplete="off"
className="form-control m-2"
value={otherReference}
onChange={(e) => { setOtherReference(e.target.value) }}
/>
<br />
</div>
{/* Submit button */}
<button type="submit" className="btn btn-success mx-3">
Next
</button>
</form>
{/* Progress indicators */}
<center>
<span className="badge rounded-pill disabled">1</span>
<span className="badge badge-pill bg-success">
<b>2</b>
</span>
<span className="badge rounded-pill disabled">3</span>
</center>
</div>
</div>
</div>
</div>
</div>
);
}
JavaScript
// EnteredDetails.js
import { useNavigate } from 'react-router-dom';
export default function EnteredDetails(props) {
const navigate = useNavigate();
// Function to handle form submission
const submit = () => {
console.log(props.data); // Log basicData object
console.log(props.questiondData); // Log questionData object
navigate('/thanks'); // Navigate to the thanks page
};
return (
<div className="container-fluid qform">
<div className="col-md-5 m-auto">
<div className="mt-3">
<div className="card text-left h-100">
<div className="card-body my-3">
<h4>Entered Details</h4>
{/* Display basicData */}
<p>
<b>Name:</b> {props.data.name}
</p>
<p>
<b>Email:</b> {props.data.email}
</p>
<p>
<b>Contact No.:</b> {props.data.contact}
</p>
<h4>Responses</h4>
{/* Display questionData */}
<p>
<b>Profession:</b> {props.questiondData.profession}
</p>
<p>
<b>Interests:</b> {props.questiondData.interest}
</p>
<p>
<b>Reference:</b> {props.questiondData.reference}
</p>
{/* Submit button */}
<button type="submit" onClick={submit} className="btn btn-success">
Submit
</button>
{/* Page numbers */}
<center>
<span className="badge rounded-pill disabled">1</span>
<span className="badge rounded-pill disabled">2</span>
<span className="badge badge-pill bg-success">
<b>3</b>
</span>
</center>
</div>
</div>
</div>
</div>
</div>
);
}
JavaScript
// ThankYouPage.js
import React from 'react';
function ThankYouPage() {
return (
<div className="container-fluid qform">
<div className="col-md-5 m-auto">
<div className="mt-3">
<div className="card text-left h-100">
<div className="card-body my-3">
<h3>Thank You for your Response!</h3>
<h6>You may close this tab now.</h6>
</div>
</div>
</div>
</div>
</div>
);
}
export default ThankYouPage;
JavaScript
// About.js
import React from 'react'
export const About = () => {
return (
<div className='text-center qform'>
<h3>This is About Section</h3>
<p>This is a survey website example using ReactJS</p>
</div>
)
}
Steps to run the application:
Step 1: To run website, open a terminal in directory of website and run following command.
npm run dev
Step 2: Open web browser and type the following URL( Default):
http://localhost:5173/
Output:
Survey Website using ReactJS
Similar Reads
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
React Fundamentals
React IntroductionReactJS is a component-based JavaScript library used to build dynamic and interactive user interfaces. It simplifies the creation of single-page applications (SPAs) with a focus on performance and maintainability.React.jsWhy Use React?Before React, web development faced issues like slow DOM updates
7 min read
React Environment SetupTo run any React application, we need to first setup a ReactJS Development Environment. In this article, we will show you a step-by-step guide to installing and configuring a working React development environment.Pre-requisite:We must have Nodejs installed on our PC. So, the very first step will be
3 min read
React JS ReactDOMReactDom is a core react package that provides methods to interact with the Document Object Model or DOM. This package allows developers to access and modify the DOM. Let's see in brief what is the need to have the package. Table of ContentWhat is ReactDOM ?How to use ReactDOM ?Why ReactDOM is used
3 min read
React JSXJSX stands for JavaScript XML, and it is a special syntax used in React to simplify building user interfaces. JSX allows you to write HTML-like code directly inside JavaScript, enabling you to create UI components more efficiently. Although JSX looks like regular HTML, itâs actually a syntax extensi
5 min read
ReactJS Rendering ElementsIn this article we will learn about rendering elements in ReactJS, updating the rendered elements and will also discuss about how efficiently the elements are rendered.What are React Elements?React elements are the smallest building blocks of a React application. They are different from DOM elements
3 min read
React ListsReact Lists are used to display a collection of similar data items like an array of objects and menu items. It allows us to dynamically render the array elements and display repetitive data.Rendering List in ReactTo render a list in React, we will use the JavaScript array map() function. We will ite
5 min read
React FormsForms are an essential part of any application used for collecting user data, processing payments, or handling authentication. React Forms are the components used to collect and manage the user inputs. These components include the input elements like text field, check box, date input, dropdowns etc.
5 min read
ReactJS KeysA key serves as a unique identifier in React, helping to track which items in a list have changed, been updated, or removed. It is particularly useful when dynamically creating components or when users modify the list. In this article, we'll explore ReactJS keys, understand their importance, how the
5 min read
Components in React
React ComponentsIn React, React components are independent, reusable building blocks in a React application that define what gets displayed on the UI. They accept inputs called props and return React elements describing the UI.In this article, we will explore the basics of React components, props, state, and render
4 min read
ReactJS Functional ComponentsIn ReactJS, functional components are a core part of building user interfaces. They are simple, lightweight, and powerful tools for rendering UI and handling logic. Functional components can accept props as input and return JSX that describes what the component should render.What are Reactjs Functio
5 min read
React Class ComponentsClass components are ES6 classes that extend React.Component. They allow state management and lifecycle methods for complex UI logic.Used for stateful components before Hooks.Support lifecycle methods for mounting, updating, and unmounting.The render() method in React class components returns JSX el
4 min read
ReactJS Pure ComponentsReactJS Pure Components are similar to regular class components but with a key optimization. They skip re-renders when the props and state remain the same. While class components are still supported in React, it's generally recommended to use functional components with hooks in new code for better p
4 min read
ReactJS Container and Presentational Pattern in ComponentsIn this article we will categorise the react components in two types depending on the pattern in which they are written in application and will learn briefly about these two categories. We will also discuss about alternatives to this pattern. Presentational and Container ComponentsThe type of compon
2 min read
ReactJS PropTypesIn ReactJS PropTypes are the property that is mainly shared between the parent components to the child components. It is used to solve the type validation problem. Since in the latest version of the React 19, PropeTypes has been removed. What is ReactJS PropTypes?PropTypes is a tool in React that he
5 min read
React Lifecycle In React, the lifecycle refers to the various stages a component goes through. These stages allow developers to run specific code at key moments, such as when the component is created, updated, or removed. By understanding the React lifecycle, you can better manage resources, side effects, and perfo
7 min read
React Hooks
Routing in React
Advanced React Concepts
React Projects