React-native-gifted-chat: How to remove a message from message list

Created on 7 Dec 2018  路  7Comments  路  Source: FaridSafi/react-native-gifted-chat

How to remove a message from message list

wontfix

Most helpful comment

No need to scare away new users @Faolain, it's a simple question.

GiftedChat stores messages in a regular array. To add a new message is in the README, copied here for reference. To remove a message will be similar. To add:

onSend(messages = []) {
  this.setState(previousState =>
    ({ messages: GiftedChat.append(previousState.messages, messages) }))
}

So to remove a message you'll need to find it in the array and delete it:

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

Apologies for formatting, I'm on mobile at the moment.

Does that help, @minicase ?

All 7 comments

Please adhere to the issue creation guidelines listed below upon creating an issue. Additionally this completely depends on what you are using for your data architecture, local state, redux etc. You can update the state of the messages being passed in using your data architectue flow of choice and it will be reflected within Gifted chat. @cooperka can this issue please be closed?

Issue Description

[FILL THIS OUT]

Steps to Reproduce / Code Snippets

[FILL THIS OUT]

Expected Results

[FILL THIS OUT]

Additional Information

  • Nodejs version: [FILL THIS OUT]
  • React version: [FILL THIS OUT]
  • React Native version: [FILL THIS OUT]
  • react-native-gifted-chat version: [FILL THIS OUT]
  • Platform(s) (iOS, Android, or both?): [FILL THIS OUT]

No need to scare away new users @Faolain, it's a simple question.

GiftedChat stores messages in a regular array. To add a new message is in the README, copied here for reference. To remove a message will be similar. To add:

onSend(messages = []) {
  this.setState(previousState =>
    ({ messages: GiftedChat.append(previousState.messages, messages) }))
}

So to remove a message you'll need to find it in the array and delete it:

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

Apologies for formatting, I'm on mobile at the moment.

Does that help, @minicase ?

Don't think it scares away users @cooperka as much as it keeps the issues for what they're intended for. If there is a need to have this information in the documentation then it should go there otherwise we will spend every other issue answering the same questions leading to an unmaintainable mess of issues that stay open for 2 years like is currently the case. If anything we should have an example usage page beyond our code examples so that new users can get acclimated to the API surface if we're trying to address that. We should have some documentation on gitbook similar to https://redux.js.org/ which would help save time while also creating a place where other users can submit documentation PRs based on newly found ways of doing things.

That's a good idea. Do you mind filing an issue for it? Or a PR if you're interested.

onDelete

thanks

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

But i want to delete on click of item

Was this page helpful?
0 / 5 - 0 ratings