Skip to content

Interactive 测验 App

中等 约 30 分钟

你将构建什么

测验 app 那个 displays one 问题 at 时间使用多个-choice 答案. 用户选择答案, 点击下一个, and sees 它们的 score at 端使用重启 option.

学习目标

  • Manage 测验状态 (current 问题, score, 选择答案)
  • 渲染问题 and choices dynamically
  • Calculate and display 最终 score
  • Handle 测验重启

前置要求

  • useState
  • Conditional 渲染
  • 数组 indexing

步骤

  1. 1

    定义测验问题 and 状态

    创建数组的 问题对象 and 状态 for current 索引, score, and 选择答案.

    代码
    const questions = [
      { q: "What is JSX?", options: ["A framework", "A syntax extension", "A CSS tool", "A database"], answer: 1 },
      { q: "What does useState return?", options: ["A value", "An array", "An object", "A promise"], answer: 1 },
    ];
    const [current, setCurrent] = useState(0);
    const [score, setScore] = useState(0);
    const [selected, setSelected] = useState(null);
    const [finished, setFinished] = useState(false);

    预期结果

    测验数据 and 状态是 ready.

  2. 2

    渲染 current 问题

    Show current 问题 and 它的 options as clickable buttons. Highlight 选择 option.

    代码
    const question = questions[current];
    return (
      <div>
        <h2>{question.q}</h2>
        {question.options.map((opt, i) => (
          <button key={i} onClick={() => setSelected(i)}
            style={{ background: selected === i ? "lightblue" : "white" }}>
            {opt}
          </button>
        ))}
      </div>
    );

    预期结果

    问题 and options 是 displayed. 点击选择答案.

  3. 3

    Handle 下一个 button and scoring

    添加下一个 button 那个检查如果选择答案是 正确, 更新 score, and advances 到下一个问题 or finishes 测验.

    代码
    const handleNext = () => {
      if (selected === question.answer) setScore(s => s + 1);
      if (current < questions.length - 1) {
        setCurrent(c => c + 1);
        setSelected(null);
      } else {
        setFinished(true);
      }
    };

    预期结果

    下一个 advances 通过问题 and shows score at 端.

  4. 4

    Show 最终 score and 重启

    当 finished 是真, show score and 重启 button 那个 resets 所有状态.

    代码
    if (finished) {
      return (
        <div>
          <h2>Score: {score}/{questions.length}</h2>
          <button onClick={() => { setCurrent(0); setScore(0); setSelected(null); setFinished(false); }}>
            Restart
          </button>
        </div>
      );
    }

    预期结果

    最终 score 是 displayed 使用重启 option.

  5. 5

    添加状态管理与事件处理

    使用 useState 管理组件状态,为交互元素添加 onClick/onChange 事件处理。状态是 React 组件的核心,事件处理让组件响应用户操作。

    代码
    import { useState } from "react";
    
    export function MyComponent() {
      const [count, setCount] = useState(0);
      const [text, setText] = useState("");
    
      return (
        <div>
          <button onClick={() => setCount(c => c + 1)}>
            Count: {count}
          </button>
          <input
            value={text}
            onChange={(e) => setText(e.target.value)}
          />
        </div>
      );
    }

    预期结果

    点击按钮计数递增;输入框内容随键入实时更新。

自己动手试试

你学到了什么

你构建完整 interactive 测验使用状态 management for 导航, scoring, and completion. 这个项目 ties together useState, conditional 渲染, and 事件 handling in 真实 application.

相关资源

相关课程

查阅不认识的术语

新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。

查看术语表

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回主题