简体   繁体   English

在React Native中使用nativeID /与RCTRootView子视图一起使用的正确方法是什么?

[英]What is the correct way of using nativeID / working with RCTRootView subviews in React Native?

I have an existing iOS app and I am using react native's RCTRootView to draw some components for my app. 我有一个现有的iOS应用程序,并且正在使用react native的RCTRootView为我的应用程序绘制一些组件。

// swift
private lazy var RNContent: RCTRootView = {
    let mockData:NSDictionary = ["someProps": self.someProps]
    let RNContent = RCTRootView(bridge: self.bridge, moduleName: "RNContent", initialProperties: mockData as [NSObject : AnyObject])!
    return RNContent
  }()

// JS
AppRegistry.registerComponent('RNContent', () => component);

Here is what my RN hierarchy looks like 这是我的RN层次结构

<View>
  <View>
   <Text />
   <ScrollView /> <--
  <View>
</View>

I want to get a hold of this ScrollView on the native side. 我想在本地获取此ScrollView As per the docs nativeID should help. 根据文档nativeID应该会有所帮助。 However, I can't see any subviews inside the RCTRootView that I define in JS side. 但是,我看不到我在JS端定义的RCTRootView内部的RCTRootView视图。 In fact, root view only has a single subview called RCTRootContentView which does not have any further subviews available. 实际上,根视图仅具有一个称为RCTRootContentView子视图,该子视图没有任何其他可用的子视图。

So my question is what is the correct way to get hold of the subviews of RCRootView on the native side? 所以我的问题是在本机端获取RCRootView子视图的正确方法是什么?

I have tried a few things like, using the UIManager 's addUIBlock to get hold of the view register, but I think there is an obvious point I am missing related to the RCTRootView and how it works. 我已经尝试了一些方法,例如,使用UIManageraddUIBlock来获取视图寄存器,但是我认为我缺少与RCTRootView及其工作方式有关的明显点。

Interestingly, if I directly use a natively registered component (registered via RCT_EXTERN_MODULE ) in my RN hierarchy I can see that this component is detached from the RCTRootContentView and it also has a nativeID that I assigned to it on the JS side. 有趣的是,如果我在RN层次结构中直接使用一个本机注册的组件(通过RCT_EXTERN_MODULE注册),则可以看到该组件已从RCTRootContentView分离出来,并且还具有在JS端分配给它的nativeID。

 RCTExecuteOnUIManagerQueue {
      self.bridge.uiManager.addUIBlock { (uiManager: RCTUIManager?, viewRegistry:[NSNumber : UIView]?) in

        uiManager?.view(forNativeID: "SelfAssignedID", withRootTag: self. RNContent.reactTag) // nil

        viewRegistry?.forEach({ (key: NSNumber, value: UIView) in
          print("Test1: ", key, value.nativeID) // only native components has nativeID, which does makes sense because RCTRootContentView doesn't have subviews... but why not? is there a way to get hold of them? If not, why not?
        })
      }
    }

Any help would be helpful, thanks! 任何帮助都会有所帮助,谢谢!

In fact, root view only has a single subview called RCTRootContentView which does not have any further subviews available. 实际上,根视图仅具有一个称为RCTRootContentView的子视图,该子视图没有任何其他可用的子视图。

This seems like an issue to me. 对我来说,这似乎是一个问题。 The <Text/> node in JS should be creating a UITextView child in the native content view. JS中的<Text/>节点应在本机内容视图中创建UITextView子级。 Maybe try actually adding some content to <Text/> and <ScrollView/> to get it to render? 也许尝试实际向<Text/><ScrollView/>添加一些内容以使其呈现?

I want to get a hold of this ScrollView on the native side. 我想在本地获取此ScrollView。

Can you give some more context on what you're trying to do? 您可以针对您要做的事情提供更多背景信息吗? I have a feeling nativeID API isn't what you want to use. 我觉得nativeID API不是您要使用的API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 React Native 视图不使用 RCTRootView 加载? - Why React Native view doesn't load using RCTRootView? 将 React Native RCTRootView 解散回 Native ViewController - Dismiss a React Native RCTRootView back to a Native ViewController 在UITableViewCell中加载React Native视图(RCTRootView) - Loading a React Native View (RCTRootView) in a UITableViewCell 将 RCTRootView 作为子视图添加到 React-Native 中的 rootViewController - Add RCTRootView as a subview to rootViewController in React-Native 如何使用RCTRootView的React Native启动选项参数 - How to use React Native launch options parameter for RCTRootView React Native 0.66 iOS 在 AppDelegate RCTRootView init 上崩溃 - React Native 0.66 iOS crashes on AppDelegate RCTRootView init React-Native Hybrid App:嵌入RCTRootView的Pop UIViewController - React-Native Hybrid App: Pop UIViewController that embeds RCTRootView -[RCTRootView cancelTouches]` 已弃用,将很快在反应原生 map 中删除 - -[RCTRootView cancelTouches]` is deprecated and will be deleted soon in react native map React-Native与现有应用程序“RCTRootView.h”文件集成未找到 - React-Native Integrating with Existing Apps 'RCTRootView.h' file not found 我将UIView子类化。 它的某些子视图具有动画效果。 重新启动这些的正确方法是什么? - I subclassed UIView. Some of its subviews are animated. What's the correct way to restart these?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM