Open In App

Create an Interactive Quiz App Using React-Native

Last Updated : 02 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will implement an Interactive Quiz App using React Native. The Interactive Quiz App is a mobile application that allows users to take tests and view their quiz results to assess their performance. This Interactive Quiz App consists of multiple questions and multiple-choice answers to each question.

To give you a better idea of what we’re going to create, let’s watch a demo video.

Demo Video

Playground

Note: This Section is to interact with the app which you are going to build.

Prerequisites:

Step-by-Step Implementation

Step 1: Create a React Native Project

Now, create a project with the following command.

npx create-expo-app app-name --template

Note: Replace the app-name with your app name for example : react-native-demo-app

Next, you might be asked to choose a template. Select one based on your preference as shown in the image below. I am selecting the blank template because it will generate a minimal app that is as clean as an empty canvas in JavaScript.

It completes the project creation and displays a message: "Your Project is ready!" as shown in the image below.

Now go into your project folder, i.e., react-native-demo

cd app-name

Project Structure:

Step 2: Run  Application

Start the server by using the following command.

npx expo start

Then, the application will display a QR code.

For the Android users,

  • For the Android Emulator, press " a" as mentioned in the image below.
  • For the Physical Device, download the " Expo Go " app from the Play Store. Open the app, and you will see a button labeled " Scan QR Code. " Click that button and scan the QR code; it will automatically build the Android app on your device.

For iOS users, simply scan the QR code using the Camera app.

If you're using a web browser, it will provide a local host link that you can use as mentioned in the image below.

Step 3: Start Coding

Approach:

A quiz app is a software application that allows users to take tests and view their quiz results to see how well they performed. In this app, we included multiple questions, each with four multiple-choice options. Users can submit answers to the questions by clicking on the options shown on the screen. After the end of the test score will be shown on the screen with a retest button too.

Example: Write the code in the files below

  • App.js: This file imports the component and renders it on screen
  • Quiz.js: This file implements all the functionalities and imports the quiz data
  • quizData.js: This file contains the data for the quiz
App.js
// App.js
import React from 'react';
import { View, Text, StyleSheet } from 'react-native';
import Quiz from './Quiz';

const App = () => {
	return (
		<View style={{ flex: 1, }}>
			<Text style={styles.heading}>
				Interactive Quiz App
			</Text>
			<Quiz />
		</View>
	);
};

export default App;
const styles = StyleSheet.create({
	heading: {
		fontSize: 25,
		paddingVertical: 60,
		paddingHorizontal: 10,
		fontWeight: 'bold',
		color: 'green',
	}

})
Quiz.js
//Quiz.js

import React, { useState, useEffect } from 'react';
import {
	View, Text, TouchableOpacity,
	StyleSheet
} from 'react-native';

import { quizData } from './quizData'

const Quiz = () => {
	const [currentQuestion, setCurrentQuestion] =
		useState(0);
	const [score, setScore] = useState(0);
	const [quizCompleted, setQuizCompleted] =
		useState(false);
	const [timeLeft, setTimeLeft] = useState(10);

	useEffect(() => {
		const timer = setTimeout(() => {
			if (timeLeft > 0) {
				setTimeLeft(timeLeft - 1);
			} else {
				if (currentQuestion <
					quizData.length - 1) {
					setCurrentQuestion(currentQuestion + 1);
					setTimeLeft(10);
				} else {
					setQuizCompleted(true);
				}
			}
		}, 1000);

		return () => clearTimeout(timer);
	}, [currentQuestion, timeLeft]);

	const handleAnswer = (selectedOption) => {
		if (selectedOption ===
			quizData[currentQuestion].correctAnswer) {
			setScore(score + 1);
		}

		if (currentQuestion <
			quizData.length - 1) {
			setCurrentQuestion(currentQuestion + 1);
			setTimeLeft(10);
		} else {
			setQuizCompleted(true);
		}
	};

	const handleRetest = () => {
		setCurrentQuestion(0);
		setScore(0);
		setQuizCompleted(false);
		setTimeLeft(10);
	};
	// Display questions and answers when the quiz is completed
	const displayAnswers =
		quizData.map((question, index) => (
			<View key={index}>
				<Text style={styles.question}>
					Question {index + 1}:  
					{" "+quizData[index].question}
				</Text>
				<Text style={styles.correctAnswer}>
					Correct Answer: 
					{" "+quizData[index].correctAnswer}
				</Text>

			</View>
		));

	return (
		<View style={styles.container}>
			{quizCompleted ? (
				<View>
					<Text style={styles.score}>
						Your Score: {score} / 5
					</Text>
					<Text style={styles.question}>
						Questions and Answers: 
					</Text>
					{displayAnswers}
					<TouchableOpacity
						style={styles.retestButton}
						onPress={handleRetest}>
						<Text style={styles.buttonText}>
							Retest
						</Text>
					</TouchableOpacity>
				</View>
			) : (
				<View>
					<Text style={styles.question}>
						{quizData[currentQuestion].question}
					</Text>
					<Text style={styles.timer}>
						Time Left: {timeLeft} sec
					</Text>
					{quizData[currentQuestion]
						.options.map((option, index) => (
							<TouchableOpacity
								key={index}
								style={styles.option}
								onPress={() => handleAnswer(option)}
							>
								<Text style={{fontSize: 16,
		fontWeight: 'bold',
        color: 'black',}}>
									{option}
								</Text>
							</TouchableOpacity>
						))}
				</View>
			)}
		</View>
	);
};

const styles = StyleSheet.create({
	container: {
		flex: 1,
		alignItems: 'center',
	},
	question: {
		fontSize: 18,
		fontWeight: 'bold',
		marginBottom: 2,
	},
	option: {
		backgroundColor: '#DDDDDD',
		padding: 10,
		marginBottom: 10,
		alignItems: 'center',
        color: 'black',
	},
	buttonText: {
		fontSize: 16,
		fontWeight: 'bold',
        color: 'white',
	},
	score: {
		fontSize: 18,
		fontWeight: 'bold',
		marginBottom: 20,
	},
	retestButton: {
		backgroundColor: 'blue',
		padding: 10,
        borderRadius: 8,
		alignItems: 'center',
        color: 'white',
	},
	timer: {
		fontSize: 11,
		fontWeight: 'bold',
		backgroundColor: 'green',
		paddingVertical: 11,
		margin: 10,
		borderRadius: 8,
        alignItems: 'center',
        alignSelf: 'center',
        color: 'white',
        padding: 10

	},
	correctAnswer: {
		color: 'green',
        marginBottom: 10,
        fontSize: 16,
		fontWeight: 'bold',

	},

});
export default Quiz;
quizData.js
// quizData.js

export const quizData = [
	{
		question: "What is the capital of France?",
		options: ["London", "Berlin", "Paris", "Madrid"],
		correctAnswer: "Paris",
	},
	{
		question: "What is 2 + 2?",
		options: ["3", "4", "5", "6"],
		correctAnswer: "4",
	},
	{
		question: "What is the largest ocean on Earth?",
		options: ["Pacific Ocean", "Atlantic Ocean", 
				"Indian Ocean", "Arctic Ocean"],
		correctAnswer: "Pacific Ocean",
	},
	{
		question: "What is the tallest mountain on Earth?",
		options: ["Mount Everest", "Mount Kilimanjaro", 
				"Mount Fuji", "Mount Denali"],
		correctAnswer: "Mount Everest",
	},
	{
		question: "What is the chemical symbol for water?",
		options: ["H2O", "CO2", "O2", "N2"],
		correctAnswer: "H2O",
	},
];

Output


Similar Reads