gpt4 book ai didi

javascript - 如何将react-native-gifted-chat与cloud firestore一起使用?

转载 作者:行者123 更新时间:2023-12-02 22:45:21 24 4
gpt4 key购买 nike

我在使用云Firestore时遇到了react-native-gifted-chat问题。我无法获取以前的消息并附加到有天赋的聊天中。请向我展示如何与云 Firestore 一起使用的代码。谢谢

最佳答案

我已经能够使用与 GitHib repo 中找到的类似方法使其在我的应用程序上运行。

我的代码调用 componentDidMount 中的 loadMessages 函数,该函数使用 onSnapshot 来跟踪我的消息或聊天集合中的任何更改。如果发生更改,它会使用回调函数将新消息附加到 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);
});
});
}

有任何问题请留言!

关于javascript - 如何将react-native-gifted-chat与cloud firestore一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58425843/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com