I am trying to build a chat app using react native. I am using flatlist to list out the messages of a conversation.
export default class ExampleConversation extends React.Component {
_renderItem = ({item}) => {
return (
<View style={{backgroundColor: 'white', margin: 4, flexDirection: 'row'}}>
<Text>{item.sender.id}</Text>
<Text>{item.body}</Text>
</View>
)
};
render() {
return <FlatList
data={data}
renderItem={this._renderItem}
keyExtractor={(item, id) => item.id}
style={styles.container}
inverted={true}/>
}
}
JSON data fetched from the server:
const data = [
{
"id": 13,
"sender_id": 1,
"body": "Some message"
},
{
"id": 12,
"sender_id": 1,
"body": "Some message"
},
{
"id": 11,
"sender_id": 5,
"body": "Some message"
}
];
As you can see from the above data, both first and second messages are from the same sender (sender_id: 1). When the previous message and the current message are from the same sender, I would like to remove the <Text>{sender_id}</Text> for that message object.
return (
<View style={{backgroundColor: 'white', margin: 4, flexDirection: 'row'}}>
<Text>{item.body}</Text>
</View>
)
Basically, I would like to change the layout of the Message component by checking the elements given for the Flatlist data, much like how the facebook messenger displays its messages.
How can I differentiate between the previous and current elements by its key values of the Flatlist data?
