温馨提示:本文翻译自stackoverflow.com,查看原文请点击:react native - Delete and copy mesage in gifted chat

react native - 在天才聊天中删除和复制消息

发布于 2020-03-29 22:04:20

我不得不像这样将onLongPress添加到GiftedChat组件

 <GiftedChat
   onLongPress={this.onLongPress}

   ....
   ....
/>

onLongPress返回上下文,消息。然后,您可以显示一个ActionSheet并添加要删除和复制的逻辑。

onLongPress(context, message) {
    console.log(context, message);
    const options = ['copy','Delete Message', 'Cancel'];
    const cancelButtonIndex = options.length - 1;
    context.actionSheet().showActionSheetWithOptions({
        options,
        cancelButtonIndex
    }, (buttonIndex) => {
        switch (buttonIndex) {
            case 0:
                Clipboard.setString(this.props.currentMessage.text); this code not work and brokes my app :(
                break;
            case 1:
               //code to delete
                break;
        }
    });
}

在这里,我有发送代码的属性:

  sendBotResponse(text) {
let msg = {
  _id: this.state.messages.length + 1,
  text,
  //createdAt: new Date(Date.UTC(2019, 5, 11, 17, 20, 0)),
  createdAt: new Date(),
  user: BOT_USER,
};
this.setState(previousState => ({
  messages: GiftedChat.append(previousState.messages, [msg]),
 }));
}

handleGoogleResponse(result) {
  console.log(result);
  let text = result.queryResult.fulfillmentMessages[0].text.text[0];
  this.sendBotResponse(text);
}

onSend(messages = []) {
   this.setState(previousState => ({
    messages: GiftedChat.append(previousState.messages, messages),
 }));
 let message = messages[0].text;
 Dialogflow_V2.requestQuery(
    message,
    result => this.handleGoogleResponse(result),
    error => console.log(error)
  );
}

但是我没有成功,有人可以帮助我完成这段代码吗?ps。我正在使用DIALOGFLOW

查看更多

提问者
Matheus cabral
被浏览
30
Areeb Vohra 2020-02-06 23:36

更改this.props.currentMessage.text
messages.text
创建以下功能:

    onDelete(messageIdToDelete) {
    this.setState(previousState =>
      ({ messages: previousState.messages.filter(message => message.id !== messageIdToDelete) }))
    }

    onLongPress(context, message) {
    console.log(context, message);
    const options = ['copy','Delete Message', 'Cancel'];
    const cancelButtonIndex = options.length - 1;
    context.actionSheet().showActionSheetWithOptions({
        options,
        cancelButtonIndex
    }, (buttonIndex) => {
        switch (buttonIndex) {
            case 0:
                Clipboard.setString(message.text);
                break;
            case 1:
                this.onDelete(messageIdToDelete) //pass the function here
                break;
        }
    });
}

发布
问题

分享
好友

手机
浏览

扫码手机浏览