0

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?

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much! But now I get this error: Hashtags(...): Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null. Why I get that? I have return statement, why it is null?
I think it may be because you haven't return anything from your Hashtags component. you should wrap the hashtags.map function with a return statement. like this return( hashtags.map((item,i) =>{ return( "Checkbox logics here!!" ) } )
Thank you so much!
0

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);
}

1 Comment

Thank you very much! But now I get this error: Hashtags(...): Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null. Why I get that? I have return statement, why it is null?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.