使用 Dialogflow,我有 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;
}
});
}
这里我有代码的发送属性:
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 中使用箭头函数