[英]Error: Too many re-renders. React limits the number of renders to prevent an infinite loop. - React
So I'm trying to make a screen where data from user's localstorage is used (Lets call it var1) but I'm getting Error: Too many re-renders. React limits the number of renders to prevent an infinite loop.
所以我正在尝试制作一个使用来自用户本地存储的数据的屏幕(让我们称之为 var1),但我收到
Error: Too many re-renders. React limits the number of renders to prevent an infinite loop.
Error: Too many re-renders. React limits the number of renders to prevent an infinite loop.
error.错误。 What I'm trying to do is check if the data from user's localstorage exists and if it does then it will put that data into a state but first it will grab another variable from localstorage (User auth token, lets call it var2) and put it into every object in var1 (var1 is a list which contains objects) and this is done using map then the state is set to the changed var1 with the auth token(or var2), then it returns some HTML and some logic is used in HTML, For every object in var1 it will create a new select tag with numbers ranging from 1 to 20 and this is done using mapping an array with 20 numbers (I'm doing this because I could not get for loop to work properly) and if the current number of option in select tag matches a key va
我要做的是检查来自用户本地存储的数据是否存在,如果存在,那么它将将该数据放入 state 但首先它将从本地存储中获取另一个变量(用户身份验证令牌,我们称之为 var2)并放入it into every object in var1 (var1 is a list which contains objects) and this is done using map then the state is set to the changed var1 with the auth token(or var2), then it returns some HTML and some logic is used in HTML,对于 var1 中的每个 object,它将创建一个新的 select 标记,其数字范围为 1 到 20,并且使用映射一个具有 20 个数字的数组无法正确执行此操作,因为我无法执行此循环到我的工作)如果 select 标签中的当前选项数与键 va 匹配lue pair in one of var1's object then it will select the option tag or put selected attribute on option tag and if you change the value of select tag then it will trigger a function which will map through var1 and select the object which user requested and change the value of quantity to whatever the user selected on select tag.
lue pair in one of var1's object then it will select the option tag or put selected attribute on option tag and if you change the value of select tag then it will trigger a function which will map through var1 and select the object which user requested and change数量值是用户在 select 标签上选择的任何值。 I tried to cut down and simplify my code as much as I could.
我试图尽可能地减少和简化我的代码。 My code is like this:
我的代码是这样的:
function RandomScreen() {
const [var1, setvar1] = useState([])
let localstoragevar = JSON.parse(localStorage.getItem('var'))
let newCart = []
if (localstoragevar) {
localstoragevar.map(item => {
item.authtoken = localStorage.getItem('AuthToken')
newCart.push(item)
})
}
setvar1(newCart)
let twenty = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
return (
{var1.map(newItem => {
{/* HTML code goes here */}
{twenty.map(number => {
if (number == item.quantity) {
return (
<option onChange={handleClick} selected name={newItem.id} value={newItem.quantity}>{newItem.quantity}</option>
)
} else {
return (
<option onChange={handleClick} name={newItem.id} value={number}>{number}</option>
)
}
})}
})}
)
}
Your render calls setvar1
which in turn trigger's a re-render.您的渲染调用
setvar1
进而触发重新渲染。 You should put this whole logic inside a useEffect
hook:您应该将整个逻辑放在
useEffect
挂钩中:
useEffect(() => {
let localstoragevar = JSON.parse(localStorage.getItem('var'))
let newCart = []
if (localstoragevar) {
localstoragevar.map(item => {
item.authtoken = localStorage.getItem('AuthToken')
newCart.push(item)
})
}
setvar1(newCart)
}, []);
This is what you have to do is to avoid logic in your render function.这就是您要做的就是避免渲染 function 中的逻辑。 For that case, we do have useEffect function plus on top of that you may add useMemo;
对于这种情况,我们确实有 useEffect function 加上您可以添加 useMemo;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.