useMessages hook

the useMessages hook is used to interface with messages belonging to a Chat.

add to imports section
import { useMessages } from '@minchat/reactnative';

Get Messages

Messages retrieved in a conversation are paginated to 25 messages at a time.

const {
         messages,
         loading,
         error,
         paginate,
         paginateLoading,
         sendMessage,
         updateMessage,
         deleteMessage

      } = useMessages(chat)
FieldTypeDescription

messages

the list of messages returned. this is automatically updated whenever a new message is received for the current conversation.

loading

boolean

Shows the state of querying the messages, used to update your UI to show loading state.

error

object

an error object that is defined if an error occured while trying to query the messages.

sendMessage

function

function called to send a message to the conversation.

updateMessage

function

function called to update a specific message in a conversation.

deleteMessage

function

function called to delete a message in a conversation.

paginate

function

function called to get the next 25 messages, the messages are automatically added to the begining of the messages array.

paginateLoading

boolean

Shows the state of a paginated query for messages, used to update your UI to show pagination state. is true when the paginate function is called and is false when messages are returned.

Send Message

To send a message, call the sendMessage(...) function from useMessages hook.

const message = {
    text: "Hello World!"
}

const callback = (data)=> { /**      */}

sendMessage(message, callback) 

New messages received are automatically added to the messages variable of useMessages(...)

Message Parameters

FieldTypeStateDescription

text

string

optional

the text of the message

file

File

optional

upload a file as an attachment

metadata

json object

optional

an optional key value pair for any additional message information such as custom font size, font type, or JSON formatted string. accepts string,number and boolean values

Callback

FieldTypeDescription

data

the message that was sent

Update Message

To update a message, call the updateMessage(...) function from useMessages hook.

const message = {
    text: "Updated Hello World!"
}

const callback = (data)=> { /**    
    do something
 */}

updateMessage(messageId, message, callback) 

messages are automatically updated in the messages variable of useMessages(...)

Parameters

FieldTypeStateDescription

messageId

string

required

the id of the message to be updated

Message Parameters

FieldTypeStateDescription

text

string

optional

the text of the message

file

File

optional

upload a file as an attachment

metadata

json object

optional

an optional key value pair for any additional message information such as custom font size, font type, or JSON formatted string. accepts string,number and boolean values

Callback

FieldTypeDescription

data

the message that was updated

Delete Message

To delete a message, call the deleteMessage(...) function from useMessages hook.

deleteMessage(messageId)  

Parameters

FieldTypeDescription

messageId

string

the Id of the message to delete

Last updated