Skip to content

Commit 01cd89f

Browse files
authored
docs: provide guide on how to add custom data to message objects (#1918)
1 parent 5977466 commit 01cd89f

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed
15.7 KB
Loading

docusaurus/docs/React/guides/customization/override-submit-handler.mdx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ title: Input Submit Handler
55
---
66

77
import SubmitHandler from '../../assets/SubmitHandler.png';
8+
import MessageCustomPayload from '../../assets/message-payload-custom-fields.png';
89

910
In this example, we demonstrate how to provide custom logic and override the [`MessageInput`](../../components/message-input-components/message-input.mdx)
1011
component's default submit handler function. Our custom handler function will bold any message text.
@@ -107,3 +108,42 @@ const App = () => (
107108
### The Result
108109

109110
<img src={SubmitHandler} alt='Override Submit Handler' width='700' />
111+
112+
113+
## Adding custom message data
114+
115+
If you would like to store custom data on your message objects, you can pass additional parameters to the [`sendMessage`](../../components/contexts/channel-action-context.mdx#sendmessage) function you retrieve from `ChannelActionContext`.
116+
117+
Now, the `overrideSubmitHandler` implementation may change as follows:
118+
119+
```tsx
120+
import React from 'react';
121+
import {
122+
ChannelHeader,
123+
MessageList,
124+
MessageInput,
125+
Window,
126+
MessageToSend,
127+
useChannelActionContext,
128+
} from 'stream-chat-react';
129+
130+
const ChannelInner = () => {
131+
const { sendMessage } = useChannelActionContext();
132+
133+
const overrideSubmitHandler = (message: MessageToSend, cid: string) => {
134+
sendMessage(message, { important: true });
135+
};
136+
137+
return (
138+
<Window>
139+
<ChannelHeader />
140+
<MessageList />
141+
<MessageInput focus overrideSubmitHandler={overrideSubmitHandler}/>
142+
</Window>
143+
)
144+
}
145+
```
146+
147+
In your browser's Developer tools Network tab you can now observe, that the message payload includes the custom field:
148+
149+
<img src={MessageCustomPayload} alt={'Custom message payload'} />

0 commit comments

Comments
 (0)