CheckBoxes React Native Map function returns error: Too many re-renders. React limits the number of renders to prevent an infinite loop

huangapple go评论69阅读模式
英文:

CheckBoxes React Native Map function returns error: Too many re-renders. React limits the number of renders to prevent an infinite loop

问题

Here is the translated code portion:

我有这段代码应该绘制复选框

这是我的 Hashtags.js 文件

import React, { useState } from 'react';
import { StyleSheet, View, Image } from 'react-native';
import { CheckBox } from 'react-native-elements';
const HashtagsList = [
  { checked: false, title: '#tip1' },
  { checked: false, title: '#tip2' },
  { checked: false, title: '#tip3' },
  { checked: false, title: '#tip4' },
  { checked: false, title: '#tip5' },
  { checked: false, title: '#tip6' },
];

const Hashtags = (props) => {
  const [hashtags, setHastags] = useState([]);
  setHastags(HashtagsList);

  const toggleCheckbox = (title) => {
    const checkedHashtags = hashtags.find((hashtag) => hashtag.title === title);
    checkedHashtags.checked = !checkedHashtags.checked;
    const checkboxes = Object.assign({}, hashtags, checkedHashtags);
    setHastags({ checkboxes });
  };

  hashtags.map((hashtag, i) => {
    console.log(hashtag);
    return (
      <CheckBox
        key={i}
        title={hashtag.title}
        checkedIcon={<Image style={styles.chekBoxPic} source={require('../assets/svg/checked.svg')} />}
        uncheckedIcon={<Image style={styles.chekBoxPic} source={require('../assets/svg/unchecked.svg')} />}
        checked={hashtag.checked}
        onPress={() => toggleCheckbox(hashtag.title)}
      />
    );
  });
};
const styles = StyleSheet.create({
  chekBoxPic: {
    width: 22,
    height: 22,
  },
});
export default Hashtags;

我的 main.js 页面如下

...
<View type={styles.someStyle}>
  <Hashtags />
</View>
...

但我收到错误消息:“重新渲染次数过多React 限制重新渲染的次数以防止无限循环。”我犯了什么错误

Please note that the error you're encountering, "Too many re-renders," is likely due to a logic issue in your code. You may need to review your component's logic to ensure that it doesn't trigger an excessive number of renders.

英文:

I have this code, that should draw checkbox'es.

here is my Hashtags.js file:

import React, { useState } from &#39;react&#39;;
import {StyleSheet, View, Image} from &#39;react-native&#39;;
import { CheckBox } from &#39;react-native-elements&#39;;
const HashtagsList =[
{checked: false, title: &#39;#tip1&#39;},
{checked: false, title: &#39;#tip2&#39;},
{checked: false, title: &#39;#tip3&#39;},
{checked: false, title: &#39;#tip4&#39;},
{checked: false, title: &#39;#tip5&#39;},
{checked: false, title: &#39;#tip6&#39;}, 
];
const Hashtags = props =&gt; { 
const [hashtags, setHastags] = useState([]);
setHastags(HashtagsList);
const toggleCheckbox = (title) =&gt;{   
const checkedHashtags = hashtags.find((hashtag) =&gt; hashtag.title === title);
checkedHashtags.checked = !checkedHashtags.checked;
const checkboxes = Object.assign({}, hashtags, checkedHashtags);
setHastags({ checkboxes });
};
hashtags.map((hashtag, i) =&gt; {
console.log(hashtag);
return (
&lt;CheckBox
key = {i}
title = {hashtag.title}
checkedIcon={&lt;Image style={styles.chekBoxPic} source={require(&#39;../assets/svg/checked.svg&#39;)} /&gt;}
uncheckedIcon={&lt;Image style={styles.chekBoxPic} source={require(&#39;../assets/svg/unchecked.svg&#39;)} /&gt;}        
checked={hashtag.checked}
onPress={() =&gt; toggleCheckbox(hashtag.title)}
/&gt; 
);
})
};
const styles = StyleSheet.create({   
chekBoxPic:{
width: 22, 
height: 22, 
},
});
export default Hashtags;

My main.js page looks like this:

...
&lt;View type={styles.someStyle}&gt;
&lt;Hashtags /&gt;
&lt;/View&gt;
...

But i get error:'Too many re-renders. React limits the number of renders to prevent an infinite loop.'
Where I make a mistake?

答案1

得分: 2

I thinks its probably because of setHastags(HashtagsList) which cause Hashtags component to end up in an infinite loop.

you can initialize the initial HashtagsList like:

const [hashtags, setHastags] = useState(HashtagsList);

英文:

I thinks its probably because of setHastags(HashtagsList) which cause Hashtags component to end up in an infinite loop.

you can initialize the initial HashtagsList like:

const [hashtags, setHastags] = useState(HashtagsList);

答案2

得分: 0

我认为问题出在你的 HashTags 组件的前几行代码中:

const Hashtags = props => { 
  const [hashtags, setHastags] = useState([]);
 
  // 每当你的 React 组件重新渲染时,它都会设置 hashtags 的状态
  // 这会导致组件陷入无限循环
  setHastags(HashtagsList);
}

而不是分开设置初始的 hashtags,你可以在 useState 调用中初始化它,就像这样:

const Hashtags = props => { 
  const [hashtags, setHastags] = useState(HashtagsList);
}
英文:

I think the problem is in the first few lines of your HashTags component:

const Hashtags = props =&gt; { 
const [hashtags, setHastags] = useState([]);
// Every time your react component re-renders, it sets the state
// Of the hashtags. This causes the component to end up in an infinite loop
setHastags(HashtagsList);
}

Instead of setting the initial hashtags separately, you can initialize it in the useState call, like this:

const Hashtags = props =&gt; { 
const [hashtags, setHastags] = useState(HashtagsList);
}

huangapple
  • 本文由 发表于 2020年1月4日 00:00:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581624.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定