There are tons of native UI widgets out there ready to be used in the latest apps - some of them are part of the platform, others are available as third-party libraries, and still more might be in use in your very own portfolio. React Native has several of the most critical platform components already wrapped, like
TextInput, but not all of them, and certainly not ones you might have written yourself for a previous app. Fortunately, it's quite easy to wrap up these existing components for seamless integration with your React Native application.
Like the native module guide, this too is a more advanced guide that assumes you are somewhat familiar with iOS programming. This guide will show you how to build a native UI component, walking you through the implementation of a subset of the existing
MapView component available in the core React Native library.
Let's say we want to add an interactive Map to our app - might as well use
Native views are created and manipulated by subclasses of
RCTViewManager. These subclasses are similar in function to view controllers, but are essentially singletons - only one instance of each is created by the bridge. They vend native views to the
RCTUIManager, which delegates back to them to set and update the properties of the views as necessary. The
Vending a view is simple:
Note: Do not attempt to set the
backgroundColor properties on the
UIView instance that you vend through the
UIView instance you want to style in another
UIView and return the wrapper
UIView instead. See Issue 2948 for more context.
The first thing we can do to make this component more usable is to bridge over some native properties. Let's say we want to be able to disable pitch control and specify the visible region. Disabling pitch is a simple boolean, so we add this one line:
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.
Now to actually disable pitch, we set the property in JS:
This isn't very well documented though - in order to know what properties are available and what values they accept, the client of your new component needs to dig through the Objective-C code. To make this better, let's make a wrapper component and document the interface with React
Now we have a nicely documented wrapper component that is easy to work with. Note that we changed the second argument to
null to the new
MapView wrapper component. This allows the infrastructure to verify that the propTypes match the native props to reduce the chances of mismatches between the ObjC and JS code.
Next, let's add the more complex
region prop. We start by adding the native code:
Ok, this is more complicated than the simple
BOOL case we had before. Now we have a
MKCoordinateRegion type that needs a conversion function, and we have custom code so that the view will animate when we set the region from JS. Within the function body that we provide,
json refers to the raw value that has been passed from JS. There is also a
view variable which gives us access to the manager's view instance, and a
defaultView that we use to reset the property back to the default value if JS sends us a null sentinel.
You could write any conversion function you want for your view - here is the implementation for
MKCoordinateRegion via two categories on
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.
To finish up support for the
region prop, we need to document it in
propTypes (or we'll get an error that the native prop is undocumented), then we can set it just like any other prop:
Here you can see that the shape of the region is explicit in the JS documentation - ideally we could codegen some of this stuff, but that's not happening yet.
Sometimes you'll have some special properties that you need to expose for the native component, but don't actually want them as part of the API for the associated React component. For example,
Switch has a custom
onChange handler for the raw native event, and exposes an
onValueChange handler property that is invoked with just the boolean value rather than the raw event. Since you don't want these native only properties to be part of the API, you don't want to put them in
propTypes, but if you don't you'll get an error. The solution is simply to call them out via the
nativeOnly option, e.g.
So now we have a native map component that we can control easily from JS, but how do we deal with events from the user, like pinch-zooms or panning to change the visible region? The key is to declare an event handler property on
RCTMapManager, make it a delegate for all the views it vends, and forward events to JS by calling the event handler block from the native view. This looks like so (simplified from the full implementation):
You can see we're adding an event handler property to the view by subclassing
MKMapView. Then we're exposing the
onChange event handler property and setting the manager as the delegate for every view that it vends. Finally, in the delegate method
-mapView:regionDidChangeAnimated: the event handler block is called on the corresponding view with the region data. Calling the
Since all our native react views are subclasses of
UIView, most style attributes will work like you would expect out of the box. Some components will want a default style, however, for example
UIDatePicker which is a fixed size. This default style is important for the layout algorithm to work as expected, but we also want to be able to override the default style when using the component.
DatePickerIOS does this by wrapping the native component in an extra view, which has flexible styling, and using a fixed style (which is generated with constants passed in from native) on the inner native component:
RCTDatePickerIOSConsts constants are exported from native by grabbing the actual frame of the native component like so:
This guide covered many of the aspects of bridging over custom native components, but there is even more you might need to consider, such as custom hooks for inserting and laying out subviews. If you want to go even deeper, check out the actual
RCTMapManager and other components in the source code.
You can edit the content above on GitHub and send us a pull request!