使用 React 构建测验应用程序

王林

王林

2024-09-10

629人浏览

转载

使用 react 构建测验应用程序

介绍

在本教程中,我们将引导您使用 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 ;
  }

  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}"></question>
</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">
      @@##@@
      <quiz></quiz><p classname="footer">made with ❤️ by abhishek gurjar</p>
    </div>
  );
};

export default app;

该组件通过页眉和页脚构建页面,测验组件呈现在中心。

结果成分

结果组件负责在用户提交答案后显示他们的测验分数。它通过将用户的回答与正确答案进行比较来计算分数,并提供有关正确回答了多少个问题的反馈。

使用HTML,CSS,JavaScript开发Android应用程序 英文文字pdf版附源文件
使用HTML,CSS,JavaScript开发Android应用程序 英文文字pdf版附源文件

如果你了解HTML,CSS和JavaScript,您已经拥有所需的工具开发Android应用程序。本动手本书展示了如何使用这些开源web标准设计和建造,可适应任何Android设备的应用程序 - 无需使用Java。您将学习如何创建一个在您选择的平台的Andr​​oid友好的网络应用程序,然后转换与自由PhoneGap框架到一个原生的Andr​​oid应用程序。了解为什么设备无关的移动应用是未来的潮流,并开始构建应用程序,提供更

下载
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 上关注他的工作。

logo

相关文章

PHP速学视频免费教程(入门到精通)
PHP速学视频免费教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

css git

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
github中文官网入口 github中文版官网网页进入
github中文官网入口 github中文版官网网页进入

github中文官网入口https://docs.github.com/zh/get-started,GitHub 是一种基于云的平台,可在其中存储、共享并与他人一起编写代码。 通过将代码存储在GitHub 上的“存储库”中,你可以: “展示或共享”你的工作。 持续“跟踪和管理”对代码的更改。

2026.01.21

7595

27

GitHub官网入口版本汇总
GitHub官网入口版本汇总

本专题整合了GitHub入口版本地址汇总,阅读专题下面的文章了解更多详细内容。

2026.04.02

454

19

http500解决方法
http500解决方法

http500解决方法有检查服务器日志、检查代码错误、检查服务器配置、检查文件和目录权限、检查资源不足、更新软件版本、重启服务器或寻求专业帮助等。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

2023.11.09

1648

4

http请求415错误怎么解决
http请求415错误怎么解决

解决方法:1、检查请求头中的Content-Type;2、检查请求体中的数据格式;3、使用适当的编码格式;4、使用适当的请求方法;5、检查服务器端的支持情况。更多http请求415错误怎么解决的相关内容,可以阅读下面的文章。

2023.11.14

629

3

HTTP 503错误解决方法
HTTP 503错误解决方法

HTTP 503错误表示服务器暂时无法处理请求。想了解更多http错误代码的相关内容,可以阅读本专题下面的文章。

2024.03.12

5112

8

http与https有哪些区别
http与https有哪些区别

http与https的区别:1、协议安全性;2、连接方式;3、证书管理;4、连接状态;5、端口号;6、资源消耗;7、兼容性。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

2024.08.16

4621

6

Go 语言 HTTP 编程与中间件开发教程合集
Go 语言 HTTP 编程与中间件开发教程合集

全面讲解 Go 语言 HTTP 编程的核心知识,涵盖 net/http 标准库的 Server/Handler/HandlerFunc 体系、DefaultServeMux 路由注册与自定义路由器、Request / ResponseWriter 请求响应处理、中间件(Middleware)链式设计模式与常见中间件(日志/CORS/认证/限流)开发、http.Client 的超时配置与连接池管理、Cookie / Session 处理、

2026.05.08

174

31

墨刀AI提示词教学
墨刀AI提示词教学

本合集由PHP中文网精心整理,为您提供全面的墨刀AI提示词教学。内容涵盖高质量原型撰写公式与实操窍门,助您轻松掌握AI设计工具。无论是零基础入门还是进阶技巧,都能让您快速上手,大幅提升产品设计与协作效率。

2026.08.04

10

21

墨刀AI完整入门
墨刀AI完整入门

PHP中文网为您倾力打造墨刀AI保姆级入门指南完整版!本合集从零基础讲起,涵盖AI生成原型、提示词优化、图片转原型及多轮对话等核心功能。无论您是新手还是进阶用户,都能轻松掌握产品设计全流程。快来PHP中文网,一键解锁高效设计技巧,让想法即刻成型!

2026.08.04

8

20

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Sass 教程
Sass 教程

共14课时 | 1.3万人学习

Bootstrap 5教程
Bootstrap 5教程

共46课时 | 5万人学习

CSS教程
CSS教程

共754课时 | 84.4万人学习