You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The `requireNativeComponent` function automatically resolves `RNTMap` to `RNTMapManager` and exports our native view for use in JavaScript.
67
+
67
68
```tsx title="MyApp.tsx"
68
69
importMapViewfrom'./MapView.tsx';
69
70
70
-
...
71
-
72
-
render() {
71
+
exportdefaultfunction MyApp() {
73
72
return <MapViewstyle={{flex: 1}} />;
74
73
}
75
74
```
76
75
77
-
Make sure to use `RNTMap` here. We want to require the manager here, which will expose the view of our manager for use in JavaScript.
78
-
79
76
:::note
80
77
When rendering, don't forget to stretch the view, otherwise you'll be staring at a blank screen.
81
78
:::
82
79
83
-
```tsx
84
-
render() {
85
-
return <MapViewstyle={{flex: 1}} />;
86
-
}
87
-
```
88
-
89
-
This is now a fully-functioning native map view component in JavaScript, complete with pinch-zoom and other native gesture support. We can't really control it from JavaScript yet, though :(
80
+
This is now a fully-functioning native map view component in JavaScript, complete with pinch-zoom and other native gesture support. We can't really control it from JavaScript yet, though.
Note that we explicitly specify the type as `BOOL` - React Native uses `RCTConvert` under the hood to convert all sorts of different data types when talking over the bridge, and bad values will show convenient "RedBox" errors to let you know there is an issue ASAP. When things are straightforward like this, the whole implementation is taken care of for you by this macro.
100
91
101
-
Now to actually disable zooming, we set the property in JS:
92
+
Now to actually disable zooming, we set the property in JavaScript:
To document the properties (and which values they accept) of our MapView component we'll add a wrapper component and document the interface with React `PropTypes`:
102
+
To document the properties (and which values they accept) of our MapView component we'll add a wrapper component and document the interface with TypeScript:
* A Boolean value that determines whether the user may use pinch
123
-
* gestures to zoom in and out of the map.
111
+
* Whether the user may use pinch gestures to zoom in and out.
124
112
*/
125
-
zoomEnabled: PropTypes.bool,
126
-
};
127
-
128
-
const RNTMap =requireNativeComponent('RNTMap');
129
-
130
-
module.exports=MapView;
113
+
zoomEnabled?:boolean;
114
+
}) {
115
+
return <RNTMap{...props} />;
116
+
}
131
117
```
132
118
133
119
Now we have a nicely documented wrapper component to work with.
@@ -186,41 +172,49 @@ You could write any conversion function you want for your view - here is the imp
186
172
187
173
These conversion functions are designed to safely process any JSON that the JS might throw at them by displaying "RedBox" errors and returning standard initialization values when missing keys or other developer errors are encountered.
188
174
189
-
To finish up support for the `region` prop, we need to document it in `propTypes`:
175
+
To finish up support for the `region` prop, we can document it with TypeScript:
190
176
191
-
```tsx title="MapView.tsx"
192
-
MapView.propTypes = {
193
-
/**
194
-
* A Boolean value that determines whether the user may use pinch
195
-
* gestures to zoom in and out of the map.
196
-
*/
197
-
zoomEnabled: PropTypes.bool,
177
+
```tsx {6-25} title="MapView.tsx"
178
+
import {requireNativeComponent} from 'react-native';
179
+
180
+
const RNTMap = requireNativeComponent('RNTMap');
198
181
182
+
export default function MapView(props: {
199
183
/**
200
184
* The region to be displayed by the map.
201
185
*
202
186
* The region is defined by the center coordinates and the span of
203
187
* coordinates to display.
204
188
*/
205
-
region: PropTypes.shape({
189
+
region?: {
206
190
/**
207
191
* Coordinates for the center of the map.
208
192
*/
209
-
latitude: PropTypes.number.isRequired,
210
-
longitude: PropTypes.number.isRequired,
193
+
latitude: number;
194
+
longitude: number;
211
195
212
196
/**
213
197
* Distance between the minimum and the maximum latitude/longitude
214
198
* to be displayed.
215
199
*/
216
-
latitudeDelta: PropTypes.number.isRequired,
217
-
longitudeDelta: PropTypes.number.isRequired,
218
-
}),
219
-
};
200
+
latitudeDelta: number;
201
+
longitudeDelta: number;
202
+
};
203
+
/**
204
+
* Whether the user may use pinch gestures to zoom in and out.
205
+
*/
206
+
zoomEnabled?: boolean;
207
+
}) {
208
+
return <RNTMap {...props} />;
209
+
}
220
210
```
221
211
222
-
```tsx title="MyApp.tsx"
223
-
render() {
212
+
We can now supply the `region` prop to `MapView`:
213
+
214
+
```tsx {4-9,12} title="MyApp.tsx"
215
+
importMapViewfrom'./MapView.tsx';
216
+
217
+
exportdefaultfunction MyApp() {
224
218
const region = {
225
219
latitude: 37.48,
226
220
longitude: -122.16,
@@ -237,8 +231,6 @@ render() {
237
231
}
238
232
```
239
233
240
-
Here you can see that the shape of the region is explicit in the JS documentation.
241
-
242
234
## Events
243
235
244
236
So now we have a native map component that we can control freely from JS, but how do we deal with events from the user, like pinch-zooms or panning to change the visible region?
In the delegate method `-mapView:regionDidChangeAnimated:` the event handler block is called on the corresponding view with the region data. Calling the `onRegionChange` event handler block results in calling the same callback prop in JavaScript. This callback is invoked with the raw event, which we typically process in the wrapper component to simplify the API:
321
313
322
-
```tsx title="MapView.tsx"
323
-
class MapView extends React.Component {
324
-
_onRegionChange = event => {
325
-
if (!this.props.onRegionChange) {
326
-
return;
327
-
}
314
+
```tsx {3-10,14-17,19} title="MapView.tsx"
315
+
// ...
328
316
329
-
// process raw event...
330
-
this.props.onRegionChange(event.nativeEvent);
317
+
type RegionChangeEvent = {
318
+
nativeEvent: {
319
+
latitude: number;
320
+
longitude: number;
321
+
latitudeDelta: number;
322
+
longitudeDelta: number;
331
323
};
332
-
render() {
333
-
return (
334
-
<RNTMap
335
-
{...this.props}
336
-
onRegionChange={this._onRegionChange}
337
-
/>
338
-
);
339
-
}
340
-
}
341
-
MapView.propTypes = {
324
+
};
325
+
326
+
export default function MapView(props: {
327
+
// ...
342
328
/**
343
329
* Callback that is called continuously when the user is dragging the map.
0 commit comments