我正在使用 react-native-gifted-chat( https://github.com/FaridSafi/react-native-gifted-chat ) 在我的应用程序上创建一个聊天界面,我想从我的数据库加载消息,但是我如何在 react-native-gifted-chat 中指定它接收者发送的消息不是发送者,即在应用加载之前,消息被加载到 this.state.messages
,根据文档,这应该是这样的
{
_id: 1,
text: 'My message',
createdAt: new Date(Date.UTC(2016, 5, 11, 17, 20, 0)),
user: {
_id: 2,
name: 'React Native',
avatar: 'https://facebook.github.io/react/img/logo_og.png',
},
image: 'https://facebook.github.io/react/img/logo_og.png',
// Any additional custom parameters are passed through
}
文档只说明了如何指定发送者的消息,没有说明接收者的消息,请问如何显示接收者的消息,即
{
_id: 2,
text: 'Receiver's message',
createdAt: new Date(Date.UTC(2016, 6, 11, 17, 20, 0)),
user: {
_id: 2,
name: 'Receiver',
avatar: 'https://facebook.github.io/react/img/logo_o3.png',
},
image: 'https://facebook.github.io/react/img/logo_o3.png',
}
最佳答案
https://github.com/FaridSafi/react-native-gifted-chat/blob/master/example/App.js中有一个例子你可以在那里看到一个 onReceive() 函数。
onReceive(text) {
this.setState((previousState) => {
return {
messages: GiftedChat.append(previousState.messages, {
_id: Math.round(Math.random() * 1000000),
text: text,
createdAt: new Date(),
user: {
_id: 2,
name: 'React Native',
// avatar: 'https://facebook.github.io/react/img/logo_og.png',
},
}),
};
});
}
你可以在收到消息时像这样触发这个函数。(当状态改变时)
关于android - 在 this.state.message 中显示接收者消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53028015/