Search code examples
firebasereact-nativegoogle-cloud-firestorereact-native-gifted-chat

Is there a way to fix the "Encountered two children with the same key" error on react native when sending gifted chat messages to firestore?


I had set up a react native group chat application using gifted chat ui with firestore database but I keep getting this error once I ran the application.

Warning: Encountered two children with the same key

enter image description here The error seems to be coming from the getMessages method

  parseMsgsFromFirestore = snapshot => {
    const { id: _id } = snapshot;
    const { text, user } = snapshot.doc.data();
    const stampInSeconds = snapshot.doc.data().timestamp.seconds;
    const timestamp = new Date(stampInSeconds * 1000);
    const message = {
      _id,
      timestamp,
      text,
      user
    };
    return message;
  };

  getMessages = callback => {
    firebase
      .firestore()
      .collection('messages')
      .orderBy('timestamp')
      .onSnapshot(snapshot => {
        const changes = snapshot.docChanges();
        // TODO: Warning: Encountered two children with the same key {Error occurs in about 5 minutes and then persists}
        changes.map(change => callback(this.parseMsgsFromFirestore(change)));
      });
  };

  sendMessages = messages => {
    // sending only specific properties to firestore
    for (let i = 0; i < messages.length; i++) {
      const { text, user } = messages[i];
      const message = {
        text,
        user,
        timestamp: firebase.firestore.Timestamp.fromDate(new Date())
      };
      this.concat(message);
    }
  };

  concat = message => {
    firebase
      .firestore()
      .collection('messages')
      .add(message);
  };

In my Chat.js file, I call that method on the componentDidMount method.

import React, { Component } from 'react';
import { GiftedChat } from 'react-native-gifted-chat';
import fireStoreDB from '../database/FirestoreDB';

export default class Chat extends Component {
  static navigationOptions = ({ navigation }) => ({
    title: navigation.getParam('name')
  });

  constructor(props) {
    super(props);
    this.state = {
      messages: []
    };
  }

  componentDidMount() {
    fireStoreDB.getMessages(message =>
      this.setState(previousState => ({
        messages: GiftedChat.append(previousState.messages, message)
      }))
    );
  }

  componentWillUnmount() {
    fireStoreDB.signUserOut();
    fireStoreDB.snapOff();
  }

  get user() {
    return {
      // gifted chat user props
      name: this.props.navigation.getParam('name'),
      _id: fireStoreDB.uid
    };
  }

  render() {
    return (
      <GiftedChat
        messages={this.state.messages}
        onSend={fireStoreDB.sendMessages}
        user={this.user}
      />
    );
  }
}

All my message keys are unique in the database, except they're all are from the same user id.  All my keys are unique in the database.


Solution

  • It looks like your data is missing unique _id fields. As you can see from this line of code https://github.com/FaridSafi/react-native-gifted-chat/blob/master/src/MessageContainer.tsx#L281

    key: item._id,
    

    That's the key it's complaining about. Make sure your messages have an _id field and make sure they are unique.

    Right now have you have User._id but the top level message should also have its own _id.

    const { text, user } = messages[i]; // Does this have _id ??