无法重新渲染 REACT

Unable to re-render REACT

提问人:Hannan Yusuf Khan 提问时间:11/12/2023 最后编辑:XPDHannan Yusuf Khan 更新时间:11/14/2023 访问量:57

问:

我是 Web 开发和学习 React 的新手。我已使用ZOD进行表单实现。我的IDE没有显示任何错误,但是当我单击提交按钮时,屏幕不会重新渲染。如果有人能帮我,那就太好了。我知道这是一个小问题,但我已经挠头一天多了。

应用程序。美国9


import { useEffect, useState } from "react";
import StudentGradingEvaluationForm, {
  StudentFormData,
} from "./Components/StudentGradingEvaluationForm";
import GPACalculator from "./Components/GPACalculator";
import "./Components/Courses";

const App = () => {
  const [formData, setFormData] = useState<StudentFormData | null>(null);
  const [isFormSubmitted, setIsFormSubmitted] = useState(false);

  const handleFormSubmitted = (data: StudentFormData) => {
    if (data) {
      setFormData(data);
      setIsFormSubmitted(true);
    }
  };

  const resetForm = () => {
    setFormData(null);
    setIsFormSubmitted(false);
  };

  return (
    <>
      <div style={{ textAlign: "center" }}>
        <h1>GPA Calculator for Individual Student</h1>
      </div>
      <div>
        {!isFormSubmitted && (
          <StudentGradingEvaluationForm onSubmit={handleFormSubmitted} />
        )}
        {isFormSubmitted && !formData && (
          <p className="text-danger">Enter correct credentials</p>
        )}
      </div>
      {formData && (
        <button className="btn btn-outline-danger" onClick={resetForm}>
          Reset Form
        </button>
      )}
      {formData && formData.courses && (
        <GPACalculator
          subject={formData.courses}
          number={formData.number}
          name={formData.name}
        />
      )}
    </>
  );
};

export default App;

StudentGradingEvaluationForm.tsx



import { z } from "zod";
import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import courses from "./Courses";

const schema = z.object({
  name: z
    .string()
    .min(3, { message: "Description should be atleast 3 characters." }),
  number: z.number().min(0).max(100),
  courses: z.enum(courses, {
    errorMap: () => ({ message: "Course is required." }),
  }),
});

export type StudentFormData = z.infer<typeof schema>;

interface Props {
  onSubmit: (data: StudentFormData) => void;
}

const StudentGradingEvaluationForm = ({ onSubmit }: Props) => {
  const { register, handleSubmit } = useForm<StudentFormData>({
    resolver: zodResolver(schema),
  });

  return (
    <form
      onSubmit={handleSubmit((data) => {
        onSubmit(data);
        console.log(onSubmit);
      })}
    >
      <div className="mb-3">
        <label htmlFor="studentName" className="form-label">
          Name of Student
        </label>
        <input
          id="studentName"
          type="text"
          className="form-control"
          {...register("name")}
        />
      </div>
      <div className="mb-3">
        <label htmlFor="courseName" className="form-label">
          Name of Course
        </label>
        <div>
          <select
            id="courseName"
            className="select-control"
            {...register("courses")}
          >
            <option value=""></option>
            {courses.map((course) => (
              <option key={course} value={course}>
                {course}
              </option>
            ))}
          </select>
        </div>
      </div>
      <div className="mb-3">
        <label htmlFor="number" className="form-label">
          Enter Number
        </label>
        <input
          id="number"
          type="number"
          className="form-control"
          {...register("number")}
        />
      </div>
      <button type="submit" className="btn btn-primary">
        Submit
      </button>
    </form>
  );
};

export default StudentGradingEvaluationForm;

Courses.ts

const courses = [
  "Mechanics of Materials - I",
  "Fluid Dynamics - I",
  "Ordinary Differential Equations",
  "Thermodynamics - II",
  "Basic Electrical and Electronics Engineering",
] as const;

export default courses;

GPACalculator.tsx


interface Props {
  subject: string;
  name: string;
  number: number;
}
const GPACalculator = ({ subject, number, name }: Props) => {
  const calculateGpa = () => {
    if (number < 100 && number >= 80) return "A";
    else if (number < 80 && number >= 65) return "B";
    else if (number < 65 && number >= 55) return "C";
    else if (number < 55 && number >= 40) return "D";
    else return "FAIL";
  };
  return (
    <div>
      <h2>GPA Calculator Result for {name}</h2>
      <p>Subject: {subject}</p>
      <p>Number: {number}</p>
      <p>GPA: {calculateGpa()}</p>
    </div>
  );
};

export default GPACalculator;

我使用了以下依赖项:

npm create [email protected]
npm i bootstrap
npm i zod
npm i [email protected]
npm i @hookform/[email protected]

VS Code 未显示任何错误。一旦我点击提交按钮,什么也没发生。我相信我的代码简单且不言自明。期待帮助。

reactjs 打字稿 react-hooks react-hook-form zod

评论

0赞 ihormillion 11/12/2023
如果您尝试添加 2 个 console.logs,会发生什么情况:一个在 like 中,下一个恰好在返回之前,类似于 ?handleFormSubmittedconsole.log(data)console.log(formData)
0赞 Hannan Yusuf Khan 11/12/2023
如果我添加控制台 .log ,什么也没发生。但是当我在返回之前添加console.log时,“null”会显示在控制台上。但是当我单击“提交”按钮时,不会提示任何操作handleFormSubmitted
0赞 ihormillion 11/13/2023
因此,问题不在于应用程序不重新渲染,而在于从未调用过您的 handelFormSubmitd。

答: 暂无答案