通过节点ID查找元素 [英] Find element by Node ID
问题描述
我正在构建一个本机应用程序,并且遇到了尝试通过节点ID查找元素的问题.
某些上下文:
我正在使用具有handleFormFocus函数的注册表单的表单库.只要任何输入集中,就会触发此操作.提供给该函数的参数是事件和组件ID(整数),这两个参数都没有给我任何有用的信息,有关输入实际上是针对哪个输入的(例如电子邮件"),而是给我留下了一个ID.>
当我使用本机0.34时,我可以执行以下操作:
const ReactNativeComponentTree = require('react/lib/ReactNativeComponentTree');
let targetComponent = ReactNativeComponentTree.getInstanceFromNode(component);
let inputRef = targetComponent._currentElement.props.fieldRef;
,但由于更新为0.41,因此不再起作用.我收到以下错误:
Unable to resolve module react/lib/ReactNativeComponentTree
任何人都知道如何通过本机0.41通过其id查找元素吗?
感谢您的时间!
反应0.41 +:
import ReactNativeComponentTree from 'react-native/Libraries/Renderer/src/renderers/native/ReactNativeComponentTree';
反应0.51 +:
import ReactNativeComponentTree from 'react-native/Libraries/Renderer/shims/ReactNativeComponentTree';
I'm in the middle of building a react-native app and running into problems trying to find an element by it's Node ID.
Some context:
I'm using a Form library for my register form that has a handleFormFocus function. This gets triggered anytime any one of the inputs are focused. The parameters provided to that function are the event and the component ID (integer number), neither of which give me any useful information about which input was actually focused (i.e. "email"), but instead leave me with only an ID.
When I was using react-native 0.34 I was able to do the following:
const ReactNativeComponentTree = require('react/lib/ReactNativeComponentTree');
let targetComponent = ReactNativeComponentTree.getInstanceFromNode(component);
let inputRef = targetComponent._currentElement.props.fieldRef;
but since updating to 0.41 this no longer works. I get the following error:
Unable to resolve module react/lib/ReactNativeComponentTree
Anyone know how to accomplish finding an element by its id with react native 0.41?
Thanks for your time!
React 0.41+:
import ReactNativeComponentTree from 'react-native/Libraries/Renderer/src/renderers/native/ReactNativeComponentTree';
React 0.51+:
import ReactNativeComponentTree from 'react-native/Libraries/Renderer/shims/ReactNativeComponentTree';
这篇关于通过节点ID查找元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!