环境:expo 36.0.0 / React Native 0.61 / react-native-gifted-chat 0.13.0 / firebase 7.6.0
我的目标:仅在 Gifted Chat UI 上呈现特定于特定聊天频道的消息。
预期结果:只有与给定频道有关的消息才应显示在 Gifted Chat UI 上。
实际结果 :
- 当我从一个聊天频道导航到另一个聊天频道时,总消息会累积。
- 当我回到较早的频道时,同一聊天频道的消息会重复不止一次。
- 警告:遇到两个孩子用同一个钥匙,
%s
。密钥应该是唯一的,以便组件在更新时保持其身份。非唯一键可能会导致子项被复制和/或省略 - 该行为不受支持,并且可能在未来版本中更改。%s
到目前为止我尝试了什么?1. 每次用户使用 componentDidUpdate 从一个频道导航到另一个频道时,重新订阅新的聊天频道。2. 每次用户更改聊天频道时,将消息数组的状态设置为空数组。3.在Firebase中退订上一个节点,在componentDidUpdate中订阅一个新节点。这里的节点代表 Firebase 中由 ID 标识的聊天频道。每个节点都包含与该特定聊天频道相关的所有消息的子节点。
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);