简体   繁体   English

React js:传播不可迭代实例的无效尝试。 为了可迭代,非数组对象必须有一个 [Symbol.iterator]() 方法

[英]React js : Invalid attempt to spread non-iterable instance. In order to be iterable, non-array objects must have a [Symbol.iterator]() method

I'm trying to make gpa calculator that need letter grade and credit hours to calculate gpa.我正在尝试制作需要字母等级和学分来计算 gpa 的 gpa 计算器。 Letter grade help to find points for eg A+ means 4.00 or D means 1.00.字母等级有助于找到例如 A+ 表示 4.00 或 D 表示 1.00 的分数。

The problem is that when I change my grade value more than once I get this error and my error line is always in handlePoints() function The exact lines are问题是,当我多次更改成绩值时,我收到此错误,并且我的错误行始终位于handlePoints() function 确切的行是

const newPoints = [...points];
newPoints[i] = 4;
setPoints(...newPoints);

Below is all code I have以下是我拥有的所有代码

import React, { useEffect, useState } from "react";
import SgpaComponent from "./SgpaComponent";

function Sgpa() {
  const [subjects, setSubjects] = useState(["Subject Name"]);
  const [grades, setGrades] = useState(["A+"]);
  const [points, setPoints] = useState([4]);
  const [credits, setCredits] = useState([3]);
  const [sgpa, setSgpa] = useState();
  function handleAdd() {
    setSubjects((prev) => [...prev, "Subject Name"]);
    setGrades((prev) => [...prev, "A+"]);
    setPoints((prev) => [...prev, 4]);
    setCredits((prev) => [...prev, 3]);
  }
  useEffect(() => {
    handlePoints();
    calcSgpa();
  });

  function calcSgpa() {
    let totalCredits = 0;
    let totalPoints = 0;
    for (let i = 0; i < credits.length; i++) {
      totalCredits += credits[i];
    }
    for (let i = 0; i < points.length; i++) {
      totalPoints += points[i];
    }
    setSgpa((totalCredits * totalPoints) / totalCredits);
  }

  function handleGrade(event, i) {
    let newGrades = [...grades];
    newGrades[i] = event.target.value;
    setGrades(...newGrades);
    
  }
  function handleCredits(event, i) {
    let newCredits = [...credits];
    newCredits[i] = event.target.value;
    setCredits(...newCredits);
  }

  function handlePoints() {
    for (let i = 0 ; i<grades.length ; i++){
    if (grades[i] === "A+" || grades[i] === "A") {
      const newPoints = [...points];
      newPoints[i] = 4;
      setPoints(...newPoints);
    }
    if (grades[i] === "A-") {
      let newPoints = [...points];
      newPoints[i] = 3.67;
      setPoints(...newPoints);
    }
    if (grades[i] === "B+") {
      let newPoints = [...points];
      newPoints[i] = 3.33;
      setPoints(...newPoints);
    }

    if (grades[i] === "B") {
      let newPoints = [...points];
      newPoints[i] = 3.0;
      setPoints(...newPoints);
    }
    if (grades[i] === "B-") {
      let newPoints = [...points];
      newPoints[i] = 2.67;
      setPoints(...newPoints);
    }
    if (grades[i] === "C+") {
      let newPoints = [...points];
      newPoints[i] = 2.33;
      setPoints(...newPoints);
    }
    if (grades[i] === "C") {
      let newPoints = [...points];
      newPoints[i] = 2.0;
      setPoints(...newPoints);
    }
    if (grades[i] === "C-") {
      let newPoints = [...points];
      newPoints[i] = 1.67;
      setPoints(...newPoints);
    }
    if (grades[i] === "D+") {
      let newPoints = [...points];
      newPoints[i] = 1.37;
      setPoints(...newPoints);
    }
    if (grades[i] === "D") {
      let newPoints = [...points];
      newPoints[i] = 1.0;
      setPoints(...newPoints);
    }
    if (grades[i] === "F") {
      let newPoints = [...points];
      newPoints[i] = 0;
      setPoints(...newPoints);
    }
    }
  }

  return (
    <>
      <h3>Sgpa : {sgpa}</h3>
      {subjects.map((subject, i) => {
        return (
          <SgpaComponent
            subject={subject}
            grade={grades[i]}
            credit={credits[i]}
            point={points[i]}
            index={i}
            handleGrade={handleGrade}
            handleCredits={handleCredits}
          />
        );
      })}
      <button onClick={handleAdd}>+</button>
    </>
  );
}

export default Sgpa;

The problem is on this line (repeated multiple times):问题出在这一行(重复多次):

setPoints(...newPoints)

it should be:它应该是:

setPoints(newPoints)

newPoints is an array, and contains the new array you want assigned to the points state variable - so calling setPoints with that array as argument is exactly what you want. newPoints是一个数组,包含您要分配给points state 变量的新数组 - 因此使用该数组作为参数调用setPoints正是您想要的。

Whereas when you use the "spread" operator, you are calling setPoints with several arguments, one for each element in the array.而当您使用“spread”运算符时,您使用多个 arguments 调用setPoints ,数组中的每个元素一个。 Since React's "state update" functions only accept a single argument, you're effectively setting the state to just the first element of the array.由于 React 的“状态更新”函数只接受一个参数,因此您实际上是将 state 设置为数组的第一个元素。 Since that's not itself an array, this means the next time this code is executed, it's trying to "spread" (use ... ) on a plain number such as 4 , hence the error message.由于它本身不是一个数组,这意味着下次执行此代码时,它会尝试在诸如4之类的普通数字上“传播”(使用... ),因此会出现错误消息。

When you set your state like setPoints(...newPoints);当您将 state 设置为setPoints(...newPoints); you essentially cause the state to change from holding an array to an element.您实际上导致 state 从保存数组更改为元素。 So the next time you call it you don'thave an array and hence you can't spread it所以下次你调用它时你没有数组,因此你不能传播它

The correct way to set state is to use setPoints(newPoints) .设置 state 的正确方法是使用setPoints(newPoints) The same thing needs to be done for setGrades(...newGrades); setGrades(...newGrades);也需要做同样的事情。 and setCredits(...newCredits);setCredits(...newCredits); which would change to setGrades(newGrades);这将更改为setGrades(newGrades); and setCredits(newCredits);setCredits(newCredits);

Also all instances of setPoints(...newPoints);还有setPoints(...newPoints);的所有实例should be updated to setPoints(newPoints) in your code应在您的代码中更新为setPoints(newPoints)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 对不可迭代实例的解构尝试无效。 为了可迭代,非数组对象必须有一个 [Symbol.iterator]() 方法 - Invalid attempt to destructure non-iterable instance. In order to be iterable, non-array objects must have a [Symbol.iterator]() method 获取错误为“传播不可迭代实例的尝试无效。 为了可迭代,非数组对象必须有一个 [Symbol.iterator]() 方法&#39; - Getting error as 'Invalid attempt to spread non-iterable instance. In order to be iterable, non-array objects must have a [Symbol.iterator]() method' TypeError:无效的解构不可迭代实例的尝试。 为了可迭代,非数组对象必须有一个 [Symbol.iterator]() - TypeError: Invalid attempt to destructure non-iterable instance. In order to be iterable, non-array objects must have a [Symbol.iterator]() TypeError:解构不可迭代实例的无效尝试。 为了可迭代,非数组对象必须有一个 [Symbol.iterator]() 方法 - TypeError: Invalid attempt to destructure non-iterable instance. In order to be iterable, non-array objects must have a [Symbol.iterator]() method 将项目添加到 Reducer 中的空数组中会给出错误“类型错误:传播不可迭代实例的尝试无效”。 - Add Items into Empty array in Reducer give Error 'TypeError: Invalid attempt to spread non-iterable instance.' Usestate + Setstate [TypeError: Invalid attempt to spread non-iterable instance.] - React Native Form Handling - Usestate + Setstate [TypeError: Invalid attempt to spread non-iterable instance.] - React Native Form Handling React Native 出现错误“传播不可迭代实例的尝试无效。” 在将数据添加到空数组时 - React Native getting error “Invalid attempt to spread non-iterable instance.” while adding data to empty array 传播不可迭代实例的尝试无效 - Invalid attempt to spread non-iterable instance React Uncaught TypeError:传播不可迭代实例的无效尝试 - React Uncaught TypeError: Invalid attempt to spread non-iterable instance 在Flatlist React Native中传播不可迭代实例的无效尝试 - Invalid attempt to spread non-iterable instance in Flatlist React Native
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM