javascript - 如何将 react-native-gifted-chat 与 Cloud Firestore 一起使用?
问题描述
我在使用 cloud firestore 时遇到了 react-native-gifted-chat 的问题。我无法获取以前的消息并附加到有天赋的聊天中。请向我展示如何与云 Firestore 一起使用的代码。谢谢
解决方案
我已经能够使用与在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如果你有任何问题!
推荐阅读
- sas - 根据条件删除行
- sql - 如何每月自动更新日期字段
- javascript - 弹出框不会转到图像顶部
- angular - 角度单元测试:无法读取未定义问题的属性“订阅”
- java - SpringBoot 应用程序在 WLS12c/Java8/RHEL fedora7.5 中失败,无法读取架构文档 http://www.springframework.org/schema/beans/spring-beans.xsd
- go - 无法使用自制软件在 mac 上安装特定版本的 go
- c# - 使用 selenium C# 运行时网页不显示任何内容
- python - 使用 pandas 组合工作表,遍历特定列,将行添加到新列表
- java - 为什么每次更改初始屏幕时我的应用程序都会从后台打开?
- database - 存储去年的每月分析数据