通过节点ID查找元素 [英] Find element by Node ID

查看:104
本文介绍了通过节点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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆