简体   繁体   English

单击提交按钮时反应显示错误

[英]react shows error when submit button click

I am new to React.我是 React 的新手。 I don't understand why it's showing an error.我不明白为什么它显示错误。 I need to repeat the array object but when It reaches the last array it does not restart.我需要重复数组 object 但是当它到达最后一个数组时它不会重新启动。
https://codesandbox.io/s/generate-quote-xpu1q https://codesandbox.io/s/generate-quote-xpu1q

index.js: index.js:

import React from "react";
import ReactDOM from "react-dom";

import App from "./App";

const rootElement = document.getElementById("root");
ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  rootElement
);

app.js:应用程序.js:

import React, { useState, useEffect } from "react";
import "./styles.css";
// import { qutoes } from "./Fetch";

export default function App() {
  // const data = qutoes;
  const [data, setData] = useState("loading");
  const [index, setIndex] = useState(0);

  const qutoesBtn = () => {
    if (index === data.length - 1) {
      setIndex(0);
    } else {
      setIndex(index + 1);
    }
  };
  useEffect(() => {
    fetch("https://type.fit/api/quotes")
      .then(res => res.json())
      .then(res2 => {
        console.log(res2.slice(0, 10));
        const lists = res2.slice(0, 10);
        setData(lists);
      });
  }, []);
  return (
    <div className="App">
      <h1>Generate Quote</h1>
      <h4>
        {data === "loading" ? (
          "loading...!!"
        ) : (
          <div>
            My qutoes is ---- <br />
            <span> {data[index].text}</span> <br /> Author is --
            {data[index].author}
          </div>
        )}
      </h4>

      <button onClick={qutoesBtn}>Generate Quote</button>
    </div>
  );
}

在此处输入图像描述

You should change your condition to update once the index reaches data.length - 1一旦索引达到data.length - 1 ,您应该更改条件以更新

if (index === (data.length - 1)) {
  setIndex(0);
}

Remember that given an array [1, 2, 3] the length is 3 , but the maximum index is 2 because arrays indexes start at 0 , so when index is equal to the data.length React is going to try to access that position giving you the error that you're experiencing.请记住,给定数组[1, 2, 3]的长度是3 ,但最大索引是2因为 arrays 索引从0开始,所以当索引等于 data.length 时,React 将尝试访问 position 给出您遇到的错误。

It is because you are not checking if the array has reached the last item in the button click, So if you don't check it in the button click then it will increment the index again before it even gets to check, So change your code to this:这是因为您没有检查数组是否已到达按钮单击中的最后一项,所以如果您没有在按钮单击中检查它,那么它将在它甚至可以检查之前再次增加索引,所以更改您的代码对此:

const qutoesBtn = () => {
    if (index === data.length - 1) {
      setIndex(0);
    } else {
      setIndex(index + 1);
    }
  };

Here -1 refers to the last item in the array这里 -1 指的是数组中的最后一项

I think you need a condition where if the array reaches the end Then start it back at zero我认为你需要一个条件,如果数组到达末尾然后从零开始

You wanna Check if the next item exceeds the length and you want to check it inside the qutoesBtn你想检查下一个项目是否超过长度并且你想在qutoesBtn中检查它

 const qutoesBtn = () => {
    setIndex(index + 1);
    if (index + 1 >= data.length) {
      setIndex(0);
    }

    console.log(index);
  };

CodeSandbox here代码沙箱在这里

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

相关问题 当我提交联系表格时,它在反应 js 中显示错误 - When I submit the contact form its shows error in react js 反应:点击提交按钮 - React: Trasform submit button on click 在 React 中单击提交按钮时如何重置表单 - How to Reset a form when I click submit button in React 如何自动点击提交按钮 - 反应 - How to automatically click submit button - react 如何重置表单当我单击提交按钮时,我使用 React 钩子做出反应 - How to Reset a form When I click submit Button I React using React hooks 点击提交按钮时,React 表单不调用提交功能 - React form not calling submit function upon submit button click 点击提交按钮,执行 function 然后提交表单 react.js - on submit button click, execute function then submit form react.js 如何防止用户对登录表单的多次单击提交按钮错误做出反应? - How to prevent user multiple click submit button error for a login form in react? React Forms-当我使用OnClick()单击Submit按钮时,输入模式验证丢失了 - React Forms - Input Pattern Validation is lost when I click the Submit Button with OnClick() 我是新手,当我点击提交按钮时什么也没发生? - I am new to react and when i click on submit button nothing happens?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM