Bridges no React Native
Lucas Santos Silva
@santoslucas
www.linkedin.com/in/lucassantossilva
Como o React Native funciona?
• Threads Nativas - main thread, lida com gestos e
views, modules, shadow node
• Thread JS - roda a engine JS em separado
Como o React Native funciona?
Como as threads interagem?
Como as threads interagem?
Bridge
• Fazem a comunicação entre o nativo e o JS
• As bridges são o core do React Native
• Assíncronas
• Mensagens serializadas
Exemplo
Bridge
Bridge
• React Native já oferece diversos componentes e
APIs
• Mas e se for necessário alguma SDK ou feature
nativa não disponível no RN?
Como implementar uma Bridge?
Como implementar uma Bridge?
• Android - Java ou Kotlin
• iOS - Objective-C ou Swift
• Dois tipos:
- Native module
- Native UI
Native Module
• Expões funções nativas para o Javascript
• Não retorna valores
- Usa-se callbacks ou promisses
- Também é possível usar EventEmitter
ToastAndroid
https://facebook.github.io/react-native/docs/
toastandroid.html
Toast Module (Android) - Java
Toast Module (Android) - Java
Toast Module (Android) - JS
AlertIOS
https://facebook.github.io/react-native/docs/
alertios.html
AlertIOS (iOS) - Objective-C
AlertIOS (iOS) - Objective-C
AlertIOS (iOS) - Objective-C
AlertIOS (iOS) - JS
Native UI
• Expões views nativas para serem usadas como
Componentes
• Recebe props do React Native
Native UI - ImageView
Native UI - ImageView
Native UI - JS
Native UI - JS
Gráfico no G1
• Não havia forma eficaz de
implementar gráficos no RN
• Libs só com Webview
• https://github.com/wuxudong/
react-native-charts-wrapper
• Android - MPAndroidChart
• iOS - charts
Gráfico no G1
Algumas dicas
• Avalie bem se você realmente precisa criar uma bridge
• Muitas dependências de projeto Android deixam o build
mais lento
Algumas dicas
• https://github.com/frostney/react-native-create-library
• https://github.com/peggyrayzis/react-native-create-bridge
Talks interessantes
• https://www.youtube.com/watch?v=GiUo88TGebs
• https://www.youtube.com/watch?v=NkbO-Vhqbl0
• https://www.youtube.com/watch?v=hDviGU-57lU
Artigos
• https://wetalkit.xyz/react-native-what-it-is-and-how-it-
works-e2182d008f5e
• https://www.logicroom.co/react-native-architecture-
explained/
• https://tadeuzagallo.com/blog/react-native-bridge/
Documentação do RN
• https://facebook.github.io/react-native/docs/native-modules-
ios.html
• https://facebook.github.io/react-native/docs/native-
components-ios.html
• https://facebook.github.io/react-native/docs/native-modules-
android.html
• https://facebook.github.io/react-native/docs/native-
components-android.html
Obrigado

Bridges no React Native - Lucas Silva