[英]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. Search
和handleSearch
没有定义,但我假设它们在你的实际代码中的某个地方。
Components are typically PascalCase
, so ASSIGN5
should be Assign5
.组件通常是
PascalCase
,因此ASSIGN5
应该是Assign5
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.