Skip to content

Commit 090d40d

Browse files
docs: updated attachment custom property guide
1 parent 2f5efd7 commit 090d40d

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

docusaurus/docs/reactnative/guides/attachment_customizations.mdx

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -335,26 +335,51 @@ export default function App() {
335335
}
336336
```
337337
338-
## Memoization handler for custom attachment properties
338+
## Handling Custom Properties On Attachment
339339
340-
When we add a custom property to the attachment format, and the data is changed around that property, the component is not rerendered by default. This should be fixed by passing a custom handler to the prop [`isAttachmentEqual`](../core-components/channel.mdx#isattachmentequal) to [Channel](../core-components/channel.mdx) component.
341-
The logic should be implemented by the user by itself and must be similar to an equality check function we pass to a [React.memo](https://reactjs.org/docs/react-api.html#reactmemo) as a second argument.
340+
Default UI components and context providers from the SDK are memoized for performance purpose, and will not trigger re-renders upon updates to
341+
custom properties on attachment. Please refer to [memoization guide](../customization/custom_components.mdx#equality) for reference.
342342
343-
Eg: Suppose we add a `customField` property to the attachment object. We want to use the value of this field from the message object and have an update in the UI every time the property's value changes. We can do this by implementing a memoization handler for this field as below:
343+
Eg: Suppose we add a `customField` property to the attachment object and use it the UI in custom `Card` component.
344+
345+
```tsx
346+
<Channel
347+
Card={attachment => {
348+
return (
349+
<View>
350+
<Text>{attachment.customField}</Text>
351+
</View>
352+
);
353+
}}
354+
/>
355+
```
356+
357+
In this example, if you try to update `customField` on particular attachment from backend (or anywhere), you will not see it updated on UI until you refresh the chat.
358+
359+
The reason being, the default memoization logic only checks for fixed set of properties on attachment, and doesn't check for custom properties.
360+
361+
This can be solved by providing a function which checks for changes in custom properties which you may have been defined on attachment.
344362
345363
```tsx
346364
import { Channel } from 'stream-chat-react-native';
347365

348-
const isAttachmentEqualHandler = (prevAttachment, nextAttachment) => {
366+
const isAttachmentEqualHandler = (prevAttachment, nextAttachment) => {
349367
const attachmentEqual = prevAttachment.customField === nextAttachment.customField;
350368
if (!attachmentEqual) return false;
351369
return true;
352370
};
353371

354372
<Channel
355-
...
373+
channel={channel}
356374
isAttachmentEqual={isAttachmentEqualHandler}
375+
Card={attachment => {
376+
return (
377+
<View>
378+
<Text>{attachment.customField}</Text>
379+
</View>
380+
);
381+
}}
357382
>
358383
{/* The underlying components */}
359-
</Channel>
384+
</Channel>;
360385
```

0 commit comments

Comments
 (0)