Skip to content

Commit 42d1eec

Browse files
Merge pull request #895 from GetStream/vishal/native-handler-docs
docs: added more examples for override native handler
2 parents 15f45c8 + 9b2ca7f commit 42d1eec

File tree

1 file changed

+51
-5
lines changed

1 file changed

+51
-5
lines changed

docusaurus/docs/reactnative/customization/native_handlers.mdx

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,66 @@ The non *core* packages call a function from the core package, `registerNativeHa
1414
If desired the native handlers can be overridden.
1515
The same function that is called internally to set the handlers can be used to override them, `registerNativeHandlers`.
1616
If the function returns the same type of data as the original function it should seamlessly work with the rest of the SDK.
17-
You should look at the default implementation to ensure any override conforms to the appropriate type definition provided by the SDK.
17+
You should look at the [default implementation](https://github.com/GetStream/stream-chat-react-native/blob/master/package/native-package/src/index.js)
18+
to ensure any override conforms to the appropriate type definition provided by the SDK.
1819

19-
Haptic feedback is used in the app to indicate certain presses, and within the image viewer to indicate zoom limits are hit.
20+
:::note
21+
This should be done outside of the component lifecycle to prevent unnecessarily re-registering the same handler repeatedly.
22+
:::
23+
24+
### Example: Override haptic feedback handler
25+
26+
Haptic feedback is used in the app to indicate certain presses, and within the image viewer to indicate when zoom limits are hit.
2027
If haptic feedback is not desired in your application you can easily remove it by registering a different handler to the function.
2128

2229
```tsx
23-
import { registerNativeHandlers } from 'stream-chat-react-native';
30+
import { registerNativeHandlers } from 'stream-chat-react-native-core';
2431

2532
registerNativeHandlers({
2633
triggerHaptic: () => null,
2734
});
2835
```
2936

30-
This should be done outside of the component lifecycle to prevent unnecessarily re-registering the same handler repeatedly.
37+
### Example: Override camera handler
38+
39+
The [`takePhoto`](#takephoto) handler is used to take a picture from the camera and use it as an image attachment while composing a message.
40+
By default, SDK uses [react-native-image-crop-picker](https://github.com/ivpusic/react-native-image-crop-picker) library for this purpose.
41+
Although it's possible to replace it with a custom implementation, using [react-native-image-picker](https://github.com/react-native-image-picker/react-native-image-picker) library in the following example:
42+
43+
```tsx
44+
import { registerNativeHandlers } from 'stream-chat-react-native-core';
45+
import { launchCamera } from 'react-native-image-picker';
46+
47+
registerNativeHandlers({
48+
takePhoto: () =>
49+
new Promise((resolve, reject) => {
50+
launchCamera(
51+
{
52+
cameraType: 'back',
53+
},
54+
(response) => {
55+
if (response.errorMessage) {
56+
resolve({ cancelled: true });
57+
return;
58+
}
59+
60+
const photo = response?.assets?.[0];
61+
if (photo?.height && photo.width && photo.uri) {
62+
resolve({
63+
cancelled: false,
64+
height: photo.height,
65+
source: 'camera',
66+
uri: photo.uri,
67+
width: photo.width,
68+
});
69+
}
70+
71+
resolve({ cancelled: true });
72+
},
73+
);
74+
}),
75+
});
76+
```
3177

3278
## Handlers
3379

@@ -89,7 +135,7 @@ A object containing two keys, `addEventListener` and `fetch`, which are function
89135

90136
**Expo:** [`@react-native-community/netinfo`](https://github.com/react-native-netinfo/react-native-netinfo)
91137

92-
### PickDocument
138+
### pickDocument
93139

94140
A function to open the document picker and return documents picked from it.
95141

0 commit comments

Comments
 (0)