{"id":1955,"date":"2020-03-20T12:00:00","date_gmt":"2020-03-20T11:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/build-a-quiz-app-in-react-step-by-step-with-source-codes-part-1\/"},"modified":"2026-07-05T03:24:48","modified_gmt":"2026-07-05T01:24:48","slug":"build-a-quiz-app-in-react-step-by-step-with-source-codes-part-1","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/build-a-quiz-app-in-react-step-by-step-with-source-codes-part-1\/","title":{"rendered":"Build a Quiz App in React \u2013 Step by Step with Source Codes ( Part 1)"},"content":{"rendered":"<p>In this tutorial, I would take you through building a simple quiz application in React. As we go through, I would be explaining the pieces of codes as well.<\/p>\n<p>We would cover the following:<\/p>\n<ul>\n<li><a href=\"#t1\">Before you begin (Prerequisites)<\/a><\/li>\n<li><a href=\"#t2\">Step 1: Setup the QuizData<\/a><\/li>\n<li><a href=\"#t3\">Step 2: Add the StyleSheet<\/a><\/li>\n<li><a href=\"#t4\">Step 3: Add the App Component<\/a><\/li>\n<li><a href=\"#t5\">Step 4: Create the Quiz Component<\/a><\/li>\n<\/ul>\n<p>Also, remember to go along with the video.<\/p>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t1\">Before you begin:<\/strong><\/h5>\n<ul>\n<li>Install Node.js<\/li>\n<li>Also install VS Code (or any other IDE you are comfortable with)<\/li>\n<li>run the command npx create-react-app quiz-app\u00a0 to create the app<\/li>\n<li>Then navigate into the quiz-app folder using the command cd quiz-app<\/li>\n<li>Start the application by running the command npm start. Then check that the application started on port 3000<\/li>\n<li>Delete all the files in the src folder<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t2\">Step 1: Set up the Questions Service (QuizData Component)<\/strong><\/h5>\n<p>The question data bank is simply a list of questions along with options and a correct answer. Each item in the data bank has<\/p>\n<ul>\n<li>an id<\/li>\n<li>a question<\/li>\n<li>options<\/li>\n<li>correct answer<\/li>\n<\/ul>\n<p>To set up the question service, create a folder named components in the src folder. Then create a file called QuizData.js in the components folder. Add the following inside. Feel free to change up the questions list, or even add more questions!<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">export const QuizData = [\n    {\n        id: 0,\n        question: `What is the capital of Nigeria?`,\n        options: [`New Delhi`, `Abuja`, `Owerri`, `Enugu`],\n        answer: `Abuja`\n    },\n    {\n        id: 0,\n        question: `What is the capital of India?`,\n        options: [`New Delhi`, `Abuja`, `Mumbai`, `Aba`],\n        answer: `New Delhi`\n    },\n    {\n        id: 0,\n        question: `What is the capital of Australia?`,\n        options: [`Melbourne`, `Akokwa`, `Owerri`, `Sydney`],\n        answer: `Sydney`\n    },\n    {\n        id: 0,\n        question: `What is the capital of Turkey?`,\n        options: [`Rijadh`, `Ankara`, `Istanbul`, `Abakaliki`],\n        answer: `Ankara`\n    },\n    {\n        id: 0,\n        question: `What is the capital of Syria?`,\n        options: [`Syria`, `Damascus`, `Anambra`, `Enugu`],\n        answer: `Damascus`\n    },\n]\n<\/pre>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t3\">Step 2: Add a Stylesheet<\/strong><\/h5>\n<p>Let&#8217;s now add a stylesheet to control the appearance of the application.\u00a0 The stylesheet is given below. So just create a file in the src folder and add the styles in it<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">.App{\n    text-align: center;\n    font-family: Arial;\n}\n\n\/* The answer selecteed by the user *\/\n.selected {\n    background-color: orange;\n}\n\n\/* The list of options *\/\n.options {\n    padding: 8px;\n    border: 2px solid #000;\n    cursor: pointer;\n}\n\nul {\n    list-style-type: none;\n}\n\nbody{\n    font-family: Verdana;\n    font-size: 18px;\n    background-color: #2980B9; \n}\n<\/pre>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t4\">Step 3: Create the Index.js File (App component)<\/strong><\/h5>\n<p>This file would be the entry point of our quiz application. I would contain the App function which would be mounted at the root element of the index.html file. You can take a look at the index.html file locate inside the public folder (but don&#8217;t modify it!)<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">import React from 'react'\nimport ReactDOM from 'react-dom'\nimport Quiz from '.\/components\/Quiz'\n\nimport '.\/styles.css';\n\nfunction App() {\n    return (\n        <span style=\"color: #007700;\">&lt;div<\/span> <span style=\"color: #0000cc;\">className=<\/span><span style=\"background-color: #fff0f0;\">\"App\"<\/span><span style=\"color: #007700;\">&gt;<\/span>\n            <span style=\"color: #007700;\">&lt;Quiz<\/span> <span style=\"color: #007700;\">\/&gt;<\/span>\n        <span style=\"color: #007700;\">&lt;\/div&gt;<\/span>\n    )\n}\n\nconst rootElement = document.getElementById('root');\nReactDOM.render(<span style=\"color: #007700;\">&lt;App&gt;&lt;\/App&gt;<\/span>, rootElement) \/\/renders the component(first parameter) as a child of the element(second parameter)\n<\/pre>\n<p>&nbsp;<\/p>\n<h5><strong>Step 4: Create the Quiz Component<\/strong><\/h5>\n<p>This is the component that would manage all the logic associated with the quiz.\u00a0 This would be a class component.<\/p>\n<p>So create a file called Quiz.js inside the components folder. The initial content is shown below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">import React, { Component } from 'react'\nimport {QuizData} from '.\/QuizData';\n\nexport class Quiz extends Component {\n    render() {\n        return (\n            <span style=\"color: #007700;\">&lt;div&gt;<\/span>\n                \n            <span style=\"color: #007700;\">&lt;\/div&gt;<\/span>\n        )\n    }\n}\n\nexport default Quiz\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Note that we have imported the QuizData component as well as the React.<\/p>\n<p>At this point, you have completed the Part 1 of out quiz application. In the next part we would now work on displaying the questions and also responding to user actions. I recommend you watch the video as well.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, I would take you through building a simple quiz application in React. As we go through, I would be explaining the pieces &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[414],"tags":[],"class_list":["post-1955","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1955","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=1955"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1955\/revisions"}],"predecessor-version":[{"id":2123,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1955\/revisions\/2123"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=1955"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=1955"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=1955"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}