Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react native giftedchat can not edit message text

I want to edit a message, but it's only updated when i changed this message _id. Can someone provide me how to do it, here is my code:

const onSend = useCallback((messagesToSend = []) => {
        if(editMessage != null){
            setEditMessage()
            const m = messagesToSend[0]
            const newMessages = [...messages]
            const index = newMessages.findIndex(mes => mes._id === editMessage._id);
            console.log('index: ', index)
            if(index > -1){
                newMessages[index].text = m.text // => ***i only edit the text***
                // newMessages[index]._id = uuid.v4() => ***this working if changed _id***
                console.log('newMessages', newMessages[index]) //  => ***new message changed it's text but the bubble not change when re-render***
                LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut)
                setMessage(newMessages)
            }
        }else{
            setReplyMessage()
            appendMessage(messagesToSend)
        }
    })

like image 775
famfamfam Avatar asked Mar 14 '26 08:03

famfamfam


1 Answers

It looks like you're using useCallback without a dependency array. This means that any dependencies that you rely on will be memoized and won't update when you run the function.

You should add messages, editMessage, and maybe setMessage and appendMessage, to the dependency array. Like so:

const onSend = useCallback(
(messageToSend = []) => etc,
[messages, editMessage, setMessage, appendMessage]);

That's the first thing I would try

like image 81
Julian K Avatar answered Mar 16 '26 20:03

Julian K