What are Native UI Components?
Native UI components are platform-specific views (Android Views or iOS UIViews) that are wrapped and exposed to React Native. They allow you to:- Use platform-specific UI widgets
- Optimize performance for complex views
- Integrate existing native UI libraries
- Access native UI features not available in React Native
Architecture
Native UI components consist of:- View Manager: Creates and manages native view instances
- View Implementation: The actual native view code
- Props: Properties that can be set from JavaScript
- Events: Callbacks that send data back to JavaScript
Android Native UI Components
Creating a View Manager
- Kotlin
- Java
Registering the View Manager
Add to yourReactPackage:
Using in JavaScript
iOS Native UI Components
Creating a View Manager
- Objective-C
- Swift
View Properties
Android Property Types
Use@ReactProp annotation to expose properties:
iOS Property Macros
Handling Events
Android Events
iOS Events
Example: Activity Indicator View Manager
Here’s a real-world example from React Native’s source code (packages/react-native/React/Views/RCTActivityIndicatorViewManager.m:28-63):Advanced Topics
View Lifecycle
Android
iOS
Commands
Expose imperative methods that can be called from JavaScript:Android
iOS
TypeScript Support
Create type-safe interfaces for your native components:Best Practices
- Keep view managers focused on view creation and property mapping
- Use events for data flowing from native to JavaScript
- Use props for data flowing from JavaScript to native
- Clean up resources in lifecycle methods
- Implement proper error handling
- Document your component’s API and props
- Consider performance implications of frequent prop updates
- Test on both platforms when building cross-platform components
- Use TypeScript for better type safety
- Follow platform-specific design guidelines