我在使用 cloud firestore 时遇到了 react-native-gifted-chat 的问题。我无法获取以前的消息并附加到有天赋的聊天中。请向我展示如何与云 Firestore 一起使用的代码。谢谢
问问题
1601 次
1 回答
1
我已经能够使用与在GitHib 存储库中找到的方法类似的方法在我的应用程序上运行它
我的代码调用 componentDidMount 中的 loadMessages 函数,该函数使用 onSnapshot 来跟踪我的 Message 或 Chats 集合中的任何更改。如果发生更改,它会使用回调函数将新消息附加到 GiftedChat。
这是我的代码:
async componentDidMount() {
this.loadMessages(message => {
this.setState(previousState => {
return {
messages: GiftedChat.append(previousState.messages, message)
};
});
});
}
async loadMessages(callback) {
var that = this;
var recipientId = this.props.navigation.getParam("recipientId");
var chatId = this.generateChatID(recipientId);
this.setState({ chatId });
firebase
.firestore()
.collection("Message")
.doc(chatId)
.collection("Chats")
.orderBy("createdAt", "asc")
.onSnapshot(function(doc) {
doc.docChanges().forEach(chat => {
var id = chat.doc.id;
chat = chat.doc.data();
const newMessage = {
_id: id,
text: chat.text,
createdAt: chat.createdAt.toDate(),
user: {
_id: chat.user._id,
name: chat.user.name,
avatar: chat.avatar
}
};
callback(newMessage);
});
});
}
Lmk如果你有任何问题!
于 2019-11-11T11:00:29.943 回答