How to use codegenNativeCommands for a RCTViewComponentView #251
CptFabulouso
started this conversation in
Libraries
Replies: 1 comment
-
brother I feel your pain, it's been 2.5 days of trying to make a basic fabric UI component and it's an absolute nightmare. None of the official docs or tutorials produce a working component, all I get is C lang compile errors, and the app just wont' build. I too have been contemplating my life choices, why oh why did I enter into the JS/TS eco system, it's hell. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
While writing I found the solution. Since I did not find it documented anywhere, I still post it for anyone struggling with the same. I've found out how to do it in this react-native-wishlist lib, I can't thank the author enough.
Solution
let's create some
RTNCenteredText
component with aclear
method.RTNCenteredTextNativeComponent.ts
spec fileRCTCenteredText.h
header file (nothing special here)RCTCenteredText.mm
fileThis component does not render anything, all we are interested is in the method call
RTNCenteredText.tsx
file wrapping the native componentAs a functional component
Or as a class component
THE PAIN
This is the original post, which I leave, because I need to share my pain and is in my opinion a kind of prove that people really don't know how to work with
codegenNativeCommands
and that the docs are misleading.TLDR:
Is there some example that uses both
codegenNativeComponent
andcodegenNativeCommands
for one component?react-native-maps lib uses
codegenNativeCommands
, but nocodegenNativeComponent
. Instead they use the oldrequireNativeComponent
rnmapbox uses
codegenNativeComponent
but nocodegenNativeCommands
, instead they specify the commands in a Native Module.The initial struggle
Let's follow this example and let's say I want to add a command, how do I do it?
Based on this documentation I assumed I should add this the RTNCenteredTextNativeComponent.ts file:
and to the RTNCenteredText.mm file
But that doesn't work, because I can't access
self.bridge
. I looked at how react-native-maps (e.g. the MapView does it and what they do is they extendRCTViewManager
instead ofRCTViewComponentView
in the header file. So how do I find the view I need? The mentioned documentation above omits the function body entirely.What I tried
So I did the most simple example e.g.
The build fails on
Undefined symbol: _RTNCenteredTextCls
So I do:
That fails during runtime on
After adding componentDescriptorProvider:
Fail happens here
So I notice I missed using the
ViewProtocol
, maybe thats' problem? So I ended up withBut the same fail as above happens
Closing Insights
So I go back to react-native-maps and I don't see them specifying the NativeComponent spec for the MapView, instead they do the old school requireNativeComponent. What got me confused is that they use the codegenNativeCommands here, but it seems like they are using it just for some polyfilling? ... never mind, all is left is to contemplate on my life choices. Nobody will read this far will they. I could share my credit credentials and no one would find out. Now lets go explain why I've spent 2 days on those few lines to add the method calls to my boss. HAHA, joking I have no boss. Just post it already. I don't want to. You don't make choices here! Yes I dooo aaaaaaaararrasrrasrar...!!!
Beta Was this translation helpful? Give feedback.
All reactions