'Grouping elements in loop using React Native

I'm listing events with their start date+time. This works well, but I want to group the event by their dates as events happen on the same day. I want to do this by assigning an extra <View> above the group with the event date.

My code currently looks like this:

renderEvents() {
   
   return this.state.data.map((post, index) => {
       return (
           <View key={post.id}>
           <Text>{post.start_dateTime}</Text>  
           <Text>{post.title}</Text>
           </View>  
       );
   });
 }

However, I cannot do an if/else statement in this return()

If I make a function to save the current handling date and loop further until a next date is found using setState, it gives me an "Error: Too many re-renders. React limits the number of renders to prevent an infinite loop."

What could be the best way to approach this issue?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source