Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamically updating message style only works on oldest and most recent message #1974

Closed
nsdub opened this issue Dec 17, 2020 · 3 comments
Closed

Comments

@nsdub
Copy link
Contributor

nsdub commented Dec 17, 2020

Issue Description

I'm trying to dynamically update the style of a message. Here's the sequence of actions...

  • A user long-presses on a message
  • Message ID is added to an array in state
  • In renderMessage I check to see if props.currentMessage._id is in the array added to state
  • If it is, change the backgroundColor of the message

This works as expected for the first and last (oldest and newest) messages in the chat. It does not work for any messages in between. I'm stumped as to why. Directionally I referred to this issue.

Any guidance would be very appreciated!

Steps to Reproduce / Code Snippets

Here's my renderMessages function...

  renderMessage = (props) => {
    const messageId = props.currentMessage._id
    let highlight = false

    // If the message has been selected by the user
    if (_.includes(this.state.selectedMessages, messageId)) {
      highlight = true
    }

    return (
      <Message
        {...props}
        containerStyle={{
          right: {
            backgroundColor: highlight ? 'green' : null,
          },
          left: {
            backgroundColor: highlight ? 'green' : null,
          },
        }}
      />
    )
  }

#### Expected Results

Any message should be able to be dynamically updated.

#### Additional Information

* Nodejs version: 15.4.0
* React version: 16.13.1
* React Native version: 0.63
* react-native-gifted-chat version: 0.16.3
* Platform(s) (iOS, Android, or both?): both
* TypeScript version: N/A
@nsdub
Copy link
Contributor Author

nsdub commented Dec 17, 2020

I was able to achieve the desired behavior with the help of this issue. Adding the following prop solved my problem:

shouldUpdateMessage={ (props, nextProps) => this.state.selectedMessages }

@nsdub nsdub closed this as completed Dec 17, 2020
@adar2378
Copy link

adar2378 commented Aug 18, 2021

shouldUpdateMessage={ (props, nextProps) => //the variable which state is changing } This helped me to solve my bubble rendering problem too, thanks @nsdub!

@asevims
Copy link

asevims commented Nov 13, 2023

I had the same issue and shouldUpdateMessage didn't help. My solution was to create a deep copy while setting messages state:

setMessages((oldMessages) =>
GiftedChat.append(oldMessages, JSON.parse(JSON.stringify(updatedMessages)))
);;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants