英文:
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 '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;
My main.js page looks like this:
...
<View type={styles.someStyle}>
<Hashtags />
</View>
...
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 => {
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 => {
const [hashtags, setHastags] = useState(HashtagsList);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论