React Native 删除和复制礼物聊天中的消息

gz5pxeao  于 2023-02-19  发布在  React
关注(0)|答案(3)|浏览(149)

使用对话流,我有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 brakes my app :(
                break;
            case 1:
               //code to delete
                break;
        }
    });
}

下面是代码的send属性:

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)
  );
}

我犯了一个非常愚蠢的错误,这只适用于箭头函数,除非你手动绑定到正常函数,所以必须使用onLongPress中的箭头函数

56lgkhnf

56lgkhnf1#

更改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;
        }
    });
}
y53ybaqx

y53ybaqx2#

请任何一个将删除消息赋给类组件以上的功能组件的代码写给功能组件

mqxuamgl

mqxuamgl3#

请更改调用函数的方式
长按={此长按}到长按={()=〉{此长按()}}

相关问题