环境:expo 36.0.0/React Native 0.61/react-native-gifted-chat 0.13.0/firebase 7.6.0
我的目标:仅在 Gifted Chat UI 上呈现特定于特定聊天 channel 的消息。
预期结果:Gifted Chat 用户界面上应仅显示与给定 channel 相关的消息。
实际结果:
- 当我从一个聊天 channel 导航到另一个聊天 channel 时,消息总数会累积。
- 当我回到较早的 channel 时,同一聊天 channel 的消息重复不止一次。
- 警告:遇到两个 child 使用相同的 key ,
%s
。 key 应该是唯一的,以便组件在更新期间保持其身份。非唯一键可能会导致子项重复和/或省略 — 该行为不受支持,可能会在未来版本中更改。%s
到目前为止我尝试了什么? 1. 每次用户使用 componentDidUpdate 从一个 channel 导航到另一个 channel 时,重新启动对新聊天 channel 的订阅。 2. 每次用户更改聊天 channel 时,将消息数组的状态设置为空数组。 3、在Firebase中取消订阅之前的节点,在componentDidUpdate中订阅一个新的节点。这里的节点表示在 Firebase 中由 ID 标识的聊天 channel 。每个节点都包含属于该特定聊天 channel 的所有消息的子节点。
async componentDidMount() {
await this.getSessionInfo();
await this.getCurrentProfile();
await this.getMessages();
};
async componentDidUpdate(prevProps) {
/* sessionID represent a chat channel and corresponding node in Firebase */
/* If user navigates from one channel to another we establish a connection with new node and get the
corresponding messages */
if (this.props.navigation.state.params.sessionID !== prevProps.navigation.state.params.sessionID) {
await this.getSessionInfo();
await this.getCurrentProfile();
/* Subscribe to new chat channel */
await this.ref();
await this.getMessages();
}
};
async componentWillUnmount() {
/* Unsubscribe from database */
await this.off();
}
/* Get messages to display after component is mounted on screen */
getMessages = () => {
this.connect(message => {
this.setState(previousState => ({
messages: GiftedChat.append(previousState.messages, message),
}))
});
}
/* Each sessionID corresponds to a chat channel and node in Firebase */
ref = () => {
return database.ref(`/sessions/${this.state.sessionID}`);
}
/* Fetch last 20 messages pertaining to given chat channel and lister to parse new incoming message */
connect = (callback) => {
this.ref()
.limitToLast(20)
.on('child_added', snapshot => callback(this.parse(snapshot)));
}
/* newly created message object in GiftedChat format */
parse = snapshot => {
const { timestamp: numberStamp, text, user } = snapshot.val();
const { key: _id } = snapshot;
const timestamp = new Date(numberStamp);
const message = {
_id,
timestamp,
text,
user,
};
return message;
};
/* function that accepts an array of messages then loop through messages */
send = (messages) => {
for (let i = 0; i < messages.length; i++) {
const { text, user } = messages[i];
const message = {
text,
user
};
this.append(message);
}
};
/* append function will save the message object with a unique ID */
append = message => this.ref().push(message);
最佳答案
根据 Brett Gregson 的建议和 Mike Kamerman 的回答,我实现了以下解决方案。
- 当用户从一个 channel 导航到另一个 channel 时,我会触发一组功能。 this.ref() 函数订阅新 channel (Firebase 中的节点)。
async componentDidUpdate(prevProps) {
if (this.props.navigation.state.params.sessionID !== prevProps.navigation.state.params.sessionID) {
await this.getSessionInfo();
await this.ref();
await this.switchSession();
}
};
- 我将消息状态(消息数组)设置为空数组以及异步/等待机制。 getMessages 函数获取新 channel 的消息。这样,属于前一组的消息被清除,UI 中没有消息堆积。
switchSession = async () => {
await this.setState({ messages: [] });
await this.getMessages();
}
关于javascript - 在多 channel 聊天应用程序中,如何显示仅属于特定聊天 channel 的消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59577188/