使用 React 构建测验应用程序
在IT行业这个发展更新速度很快的行业,只有不停止的学习,才不会被行业所淘汰。如果你是文章学习者,那么本文《使用 React 构建测验应用程序》就很适合你!本篇内容主要包括##content_title##,希望对大家的知识积累有所帮助,助力实战开发!

介绍
在本教程中,我们将引导您使用 react 构建一个有趣的交互式测验网站。这个项目是初学者练习在 react 中处理用户输入、管理状态和渲染动态内容的好方法。
项目概况
测验网站允许用户回答多项选择题并获得有关其选择的即时反馈。测验结束时,用户会看到他们的分数以及正确答案。
特征
- 互动测验:用户可以回答问题并查看他们的分数。
- 实时反馈:立即指示所选答案是否正确。
- 分数计算:在整个测验过程中跟踪分数。
- 动态内容:问题和选项从预定义列表中动态呈现。
使用的技术
- react:用于构建用户界面和管理组件状态。
- css:用于设计应用程序的样式。
- javascript:用于处理逻辑和测验功能。
项目结构
该项目的结构如下:
├── public ├── src │ ├── components │ │ ├── quiz.jsx │ │ ├── question.jsx │ ├── app.jsx │ ├── app.css │ ├── index.js │ └── index.css ├── package.json └── readme.md
关键部件
- quiz.jsx:处理测验逻辑和分数计算。
- question.jsx:显示单个问题并处理用户输入。
- app.jsx:管理布局并渲染 quiz 组件。
代码说明
测验组件
测验组件负责呈现问题、计算分数以及处理测验完成。
import { useeffect, usestate } from "react";
import result from "./result";
import question from "./question";
const data = [
{
question: "what is the capital of france?",
options: ["paris", "berlin", "madrid", "rome"],
answer: "paris",
},
{
question: "what is the capital of germany?",
options: ["berlin", "munich", "frankfurt", "hamburg"],
answer: "berlin",
},
{
question: "what is the capital of spain?",
options: ["madrid", "barcelona", "seville", "valencia"],
answer: "madrid",
},
{
question: "what is the capital of italy?",
options: ["rome", "milan", "naples", "turin"],
answer: "rome",
},
{
question: "what is the capital of the united kingdom?",
options: ["london", "manchester", "liverpool", "birmingham"],
answer: "london",
},
{
question: "what is the capital of canada?",
options: ["ottawa", "toronto", "vancouver", "montreal"],
answer: "ottawa",
},
{
question: "what is the capital of australia?",
options: ["canberra", "sydney", "melbourne", "brisbane"],
answer: "canberra",
},
{
question: "what is the capital of japan?",
options: ["tokyo", "osaka", "kyoto", "nagoya"],
answer: "tokyo",
},
{
question: "what is the capital of china?",
options: ["beijing", "shanghai", "guangzhou", "shenzhen"],
answer: "beijing",
},
{
question: "what is the capital of russia?",
options: ["moscow", "saint petersburg", "novosibirsk", "yekaterinburg"],
answer: "moscow",
},
{
question: "what is the capital of india?",
options: ["new delhi", "mumbai", "bangalore", "chennai"],
answer: "new delhi",
},
{
question: "what is the capital of brazil?",
options: ["brasilia", "rio de janeiro", "sao paulo", "salvador"],
answer: "brasilia",
},
{
question: "what is the capital of mexico?",
options: ["mexico city", "guadalajara", "monterrey", "tijuana"],
answer: "mexico city",
},
{
question: "what is the capital of south africa?",
options: ["pretoria", "johannesburg", "cape town", "durban"],
answer: "pretoria",
},
{
question: "what is the capital of egypt?",
options: ["cairo", "alexandria", "giza", "luxor"],
answer: "cairo",
},
{
question: "what is the capital of turkey?",
options: ["ankara", "istanbul", "izmir", "antalya"],
answer: "ankara",
},
{
question: "what is the capital of argentina?",
options: ["buenos aires", "cordoba", "rosario", "mendoza"],
answer: "buenos aires",
},
{
question: "what is the capital of nigeria?",
options: ["abuja", "lagos", "kano", "ibadan"],
answer: "abuja",
},
{
question: "what is the capital of saudi arabia?",
options: ["riyadh", "jeddah", "mecca", "medina"],
answer: "riyadh",
},
{
question: "what is the capital of indonesia?",
options: ["jakarta", "surabaya", "bandung", "medan"],
answer: "jakarta",
},
{
question: "what is the capital of thailand?",
options: ["bangkok", "chiang mai", "phuket", "pattaya"],
answer: "bangkok",
},
{
question: "what is the capital of malaysia?",
options: ["kuala lumpur", "george town", "johor bahru", "malacca"],
answer: "kuala lumpur",
},
{
question: "what is the capital of the philippines?",
options: ["manila", "cebu city", "davao city", "quezon city"],
answer: "manila",
},
{
question: "what is the capital of vietnam?",
options: ["hanoi", "ho chi minh city", "da nang", "hai phong"],
answer: "hanoi",
},
{
question: "what is the capital of south korea?",
options: ["seoul", "busan", "incheon", "daegu"],
answer: "seoul",
},
];
const quiz = () => {
const [currentquestion, setcurrentquestion] = usestate(0);
const [score, setscore] = usestate(0);
const [showresult, setshowresult] = usestate(false);
const [timer, settimer] = usestate(30);
const [shownextbutton, setshownextbutton] = usestate(true);
useeffect(() => {
if (timer === 0) {
handlenext();
}
const timerid = settimeout(() => settimer(timer - 1), 1000);
return () => cleartimeout(timerid);
}, [timer]);
const handleanswer = (selectedoption) => {
if (selectedoption === data[currentquestion].answer) {
setscore(score + 1);
}
setshownextbutton(true); // show the next button after answering
};
const handlenext = () => {
const nextquestion = currentquestion + 1;
if (nextquestion < data.length) {
setcurrentquestion(nextquestion);
} else {
setshowresult(true);
}
setshownextbutton(true); // hide the next button after moving to the next question
settimer(30);
};
if (showresult) {
return <result score={score} totalquestion={data.length} />;
}
return (
<div classname="quiz">
<div classname="countandtime">
<div classname="questionnumber">
question : {currentquestion + 1} <b>/</b> {data.length}
</div>
<div classname="timer">time left : {timer} seconds</div>
</div>
<question
question={data[currentquestion].question}
options={data[currentquestion].options}
onanswer={handleanswer}
onnext={handlenext}
shownextbutton={shownextbutton}
/>
</div>
);
};
export default quiz;
测验组件管理当前问题索引和分数。它还会跟踪测验何时完成,并在回答所有问题后显示最终分数。
问题成分
问题组件处理每个问题的显示并允许用户选择答案。
const question = ({ question, options, onanswer, onnext, shownextbutton }) => {
return (
<div classname="question">
<h2>{question}</h2>
{options.map((option, index) => (
<button classname="option" key={index} onclick={() => onanswer(option)}>
{option}
</button>
))}
{shownextbutton && <button classname="next" onclick={onnext}>next </button>}
</div>
);
};
export default question;
该组件采用 data 属性,其中包括问题及其选项,并动态呈现它。 handleanswer 函数处理所选选项。
应用程序组件
app 组件管理布局并渲染 quiz 组件。
import quiz from "./components/quiz";
import "./app.css";
import logo from "./assets/images/quizlogo.png";
const app = () => {
return (
<div classname="app">
<img classname="logo" src={logo} alt="logo" />
<quiz />
<p classname="footer">made with ❤️ by abhishek gurjar</p>
</div>
);
};
export default app;
该组件通过页眉和页脚构建页面,测验组件呈现在中心。
结果成分
结果组件负责在用户提交答案后显示他们的测验分数。它通过将用户的回答与正确答案进行比较来计算分数,并提供有关正确回答了多少个问题的反馈。
const result = ({ score, totalquestion }) => {
return (
<div classname='result'>
<h2>quiz complete</h2>
<p>your score is {score} out of {totalquestion}</p>
</div>
);
}
export default result;
在此组件中,分数和问题总数作为 props 传递。根据分数,该组件向用户显示一条消息,要么表扬他们正确回答所有问题,要么鼓励他们继续练习。这种动态反馈可以帮助用户了解他们的表现。
css 样式
css 确保测验的布局干净简单。它设计测验组件的样式并提供用户友好的视觉效果。
* {
box-sizing: border-box;
}
body {
background-color: #cce2c2;
color: black;
margin: 0;
padding: 0;
font-family: sans-serif;
}
.app {
width: 100%;
display: flex;
align-items: center;
justify-content: flex-start;
flex-direction: column;
}
.app img {
margin: 50px;
}
/* quiz */
.quiz {
display: flex;
flex-direction: column;
align-items: center;
width: 60%;
margin: 0 auto;
}
.countandtime {
display: flex;
align-items: center;
gap: 300px;
}
.questionnumber {
font-size: 20px;
background-color: #fec33d;
padding: 10px;
border-radius: 10px;
font-weight: bold;
}
.timer {
font-size: 18px;
background-color: #44b845;
color: white;
padding: 10px;
border-radius: 10px;
font-weight: bold;
}
/* question */
.question {
margin-top: 20px;
}
.question h2 {
background-color: #eaf0e7;
width: 690px;
padding: 30px;
border-radius: 10px;
}
.question .option {
display: flex;
margin-block: 20px;
flex-direction: column;
align-items: flex-start;
background-color: #eaf0e7;
padding: 20px;
border-radius: 10px;
font-size: 18px;
width: 690px;
}
.question .next {
font-size: 25px;
color: white;
background-color: #35bd3a;
border: none;
padding: 10px;
width: 100px;
border-radius: 10px;
margin-left: 590px;
}
/* result */
.result {
border-radius: 19px;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
width: 500px;
height: 300px;
margin-top: 140px;
background-color: #35bd3a;
color: white;
}
.result h2{
font-size: 40px;
}
.result p{
font-size: 25px;
}
.footer {
margin: 40px;
}
样式确保布局居中,并在测验选项上提供悬停效果,使其更具互动性。
安装与使用
要开始此项目,请克隆存储库并安装依赖项:
git clone https://github.com/abhishekgurjar-in/quiz-website.git cd quiz-website npm install npm start
这将启动开发服务器,并且应用程序将在 http://localhost:3000 上运行。
现场演示
在此处查看测验网站的现场演示。
结论
这个测验网站对于希望提高 react 技能的初学者来说是一个出色的项目。它提供了一种引人入胜的方式来练习管理状态、呈现动态内容和处理用户输入。
制作人员
- 灵感:该项目的灵感来自于经典问答游戏,将乐趣与学习融为一体。
作者
abhishek gurjar 是一位 web 开发人员,热衷于构建交互式且引人入胜的 web 应用程序。您可以在 github 上关注他的工作。
到这里,我们也就讲完了《使用 React 构建测验应用程序》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!
解决电脑无法开机的8个有效方法
- 上一篇
- 解决电脑无法开机的8个有效方法
- 下一篇
- Vue2 中 v-if 和 v-else-if 双条件渲染失败,如何排查问题?
-
- 文章 · 前端 | 4小时前 |
- CSSz-index层级控制全攻略
- 394浏览 收藏
-
- 文章 · 前端 | 4小时前 |
- PostCSS插件配置全攻略
- 258浏览 收藏
-
- 文章 · 前端 | 4小时前 | 背景 CSS渐变 linear-gradient radial-gradient 颜色停点
- CSS渐变色详解:linear-gradient与radial-gradient用法
- 402浏览 收藏
-
- 文章 · 前端 | 5小时前 | 主题切换 color属性 currentColor 颜色统一管理 减少重复代码
- CSScurrentColor统一颜色管理技巧
- 160浏览 收藏
-
- 文章 · 前端 | 5小时前 |
- CSS导入外部样式表方法详解
- 189浏览 收藏
-
- 文章 · 前端 | 5小时前 |
- WebCryptoAPI:JavaScript密码学实战教程
- 140浏览 收藏
-
- 文章 · 前端 | 5小时前 |
- JS对象属性变化监听全解析
- 310浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 485次学习
-
- ChatExcel酷表
- ChatExcel酷表是由北京大学团队打造的Excel聊天机器人,用自然语言操控表格,简化数据处理,告别繁琐操作,提升工作效率!适用于学生、上班族及政府人员。
- 3193次使用
-
- Any绘本
- 探索Any绘本(anypicturebook.com/zh),一款开源免费的AI绘本创作工具,基于Google Gemini与Flux AI模型,让您轻松创作个性化绘本。适用于家庭、教育、创作等多种场景,零门槛,高自由度,技术透明,本地可控。
- 3405次使用
-
- 可赞AI
- 可赞AI,AI驱动的办公可视化智能工具,助您轻松实现文本与可视化元素高效转化。无论是智能文档生成、多格式文本解析,还是一键生成专业图表、脑图、知识卡片,可赞AI都能让信息处理更清晰高效。覆盖数据汇报、会议纪要、内容营销等全场景,大幅提升办公效率,降低专业门槛,是您提升工作效率的得力助手。
- 3436次使用
-
- 星月写作
- 星月写作是国内首款聚焦中文网络小说创作的AI辅助工具,解决网文作者从构思到变现的全流程痛点。AI扫榜、专属模板、全链路适配,助力新人快速上手,资深作者效率倍增。
- 4543次使用
-
- MagicLight
- MagicLight.ai是全球首款叙事驱动型AI动画视频创作平台,专注于解决从故事想法到完整动画的全流程痛点。它通过自研AI模型,保障角色、风格、场景高度一致性,让零动画经验者也能高效产出专业级叙事内容。广泛适用于独立创作者、动画工作室、教育机构及企业营销,助您轻松实现创意落地与商业化。
- 3814次使用
-
- JavaScript函数定义及示例详解
- 2025-05-11 502浏览
-
- 优化用户界面体验的秘密武器:CSS开发项目经验大揭秘
- 2023-11-03 501浏览
-
- 使用微信小程序实现图片轮播特效
- 2023-11-21 501浏览
-
- 解析sessionStorage的存储能力与限制
- 2024-01-11 501浏览
-
- 探索冒泡活动对于团队合作的推动力
- 2024-01-13 501浏览

