简体   繁体   English

为什么我收到“未捕获的错误:重新渲染次数过多”。 在尝试使用 useState 将一个从自定义挂钩中获得的常量分配给另一个常量时?

[英]Why I am getting 'Uncaught Error: Too many re-renders.' while a try to assign a const that a got from custom hook into another const using useState?

I have made a custom hook that takes url and fetches the data in json format.我制作了一个自定义挂钩,它采用 url 并以 json 格式获取数据。 But when I am trying to assign the data into const users using use state, I getting the error: 'Uncaught Error: Too many re-renders.但是,当我尝试使用 use state 将数据分配给 const 用户时,我收到错误消息:“未捕获错误:重新渲染太多。 React limits the number of renders to prevent an infinite loop' React 限制渲染次数以防止无限循环'

Here is the component from where I am trying to assign:这是我要分配的组件:

import React, { useState } from "react";
import useFetch from "./fetchData";
import Users from "./Users";
const ASSIGN5 = () => {
  const [users, setUsers] = useState();
  const { data, isLoading, error } = useFetch(
    "https://jsonplaceholder.typicode.com/users"
  );

  setUsers(data);

  return (
    <div className="container">
      <h1 className="">Users Management App</h1>
      {isLoading && <p>Loading users...</p>}
      {error && <p>{error}</p>}

      <Search onHandleSearch={handleSearch} />
      {users && <Users users={users} />}
    </div>
  );
};

export default ASSIGN5;

And here is the useFetch hook:这是 useFetch 钩子:

import React, { useEffect, useState } from "react";

const useFetch = (url) => {
  const [data, setData] = useState([]);
  const [isloading, setIsloading] = useState(true);
  const [error, setError] = useState();

  useEffect(() => {
    fetch(url)
      .then((res) => {
        if (!res.ok) {
          throw Error("Fetching unsucessful");
        } else {
          return res.json();
        }
      })
      .then((data) => {
        setData(data);
        setIsloading(false);
        setError(null);
      })
      .catch((error) => {
        setError(error.message);
        setIsloading(false);
      });
  }, [url]);
  return { data, isloading, error };
};

export default useFetch;

But it runs fine when I use data directly without assigning but I need to because have to filter the data using functions但是当我直接使用数据而不分配时它运行良好但是我需要因为必须使用函数过滤数据

I am expecting that the data will assigned to the const users我期望数据将分配给 const 用户

Don't call state setters unconditionally in the component body or that'll trigger infinite renders.不要在组件主体中无条件地调用 state setter,否则会触发无限渲染。

It appears you don't need the users state at all because it's just an alias of the data array returned by your useFetch hook.看来您根本不需要users state,因为它只是useFetch挂钩返回的data数组的别名。

const ASSIGN5 = () => {
  const { data, isLoading, error } = useFetch(
    "https://jsonplaceholder.typicode.com/users"
  );

  return (
    <div className="container">
      <h1 className="">Users Management App</h1>
      {isLoading && <p>Loading users...</p>}
      {error && <p>{error}</p>}

      <Search onHandleSearch={handleSearch} />
      {data?.length && <Users users={data} />}
    </div>
  );
};

If you want to rename it you can use如果你想重命名它,你可以使用

const { data: users, isLoading, error } = useFetch(...);
// now you can replace `data` with `users`

Search and handleSearch weren't defined but I assume those are in your actual code somewhere. SearchhandleSearch没有定义,但我假设它们在你的实际代码中的某个地方。

Components are typically PascalCase , so ASSIGN5 should be Assign5 .组件通常是PascalCase ,因此ASSIGN5应该是Assign5

暂无
暂无

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

相关问题 太多的重新渲染。 将 switch 语句与 useState() 一起使用 - Too many re-renders. Using switch statement with useState() 为什么 useState react hook 会导致过多的重新渲染。 React 限制渲染次数以防止无限循环 - Why does useState react hook cause Too many re-renders. React limits the number of renders to prevent an infinite loop useState 和 if 语句导致错误:重新渲染太多。 React 限制渲染次数以防止无限循环 - useState and if statement causing Error: Too many re-renders. React limits the number of renders to prevent an infinite loop 反应使用状态错误:重新渲染太多。 React 限制渲染次数以防止无限循环 - react usestate Error: Too many re-renders. React limits the number of renders to prevent an infinite loop 错误:重新渲染过多。 因为我改变了 setState - Error: Too many re-renders. because i changed setState 如何解决未捕获的错误:重新渲染过多。 React 在设置状态时限制渲染次数以防止无限循环? - How do I resolve Uncaught Error: Too many re-renders. React limits the number of renders to prevent an infinite loop while setting a state? 如何修复错误未捕获的错误:重新渲染过多。 React 使用 React 限制渲染次数以防止无限循环? - How to fix error Uncaught Error: Too many re-renders. React limits the number of renders to prevent an infinite loop using react? 未捕获的错误:重新渲染太多。 React 限制渲染次数以防止无限循环。 错误 - Uncaught Error: Too many re-renders. React limits the number of renders to prevent an infinite loop. error 如何解决使用 useState 的 function 调用并获得“太多重新渲染。 反应过来……” - How to solve a function call that uses useState with getting “Too many re-renders. React…” “未捕获的错误:重新渲染过多。React 限制渲染次数以防止无限循环。” - "Uncaught Error: Too many re-renders. React limits the number of renders to prevent an infinite loop."
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM