React Native - 按节点ID查找元素 [英] React Native - Find element by Node ID

查看:2038
本文介绍了React Native - 按节点ID查找元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



一些上下文:

p>

我使用具有handleFormFocus函数的注册表单使用Form库。任何时候任何一个输入都会被聚焦,这会被触发。提供给该函数的参数是事件和组件ID(整数),这两个参数都不会给我提供有关哪个输入实际聚焦的任何有用信息(即email),而是只给我一个ID。 p>

当我使用react-native 0.34时,我可以执行以下操作:


const ReactNativeComponentTree = require('react / lib / ReactNativeComponentTree');
let targetComponent = ReactNativeComponentTree.getInstanceFromNode(component);
let inputRef = targetComponent._currentElement.props.fieldRef;



但由于更新至0.41,此操作不再有效。我得到以下错误:
无法解析模块反应/ lib / ReactNativeComponentTree



任何人都知道如何通过它的id来完成寻找一个元素与原生反应0.41?



感谢您的时间!

解决方案

React 0.41 +:

 从'react-native / Libraries / Renderer / src /渲染器/天然的/ ReactNativeComponentTree'; 

反应0.51 +:

 从'react-native / Libraries / Renderer / shims / ReactNativeComponentTree'导入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';

这篇关于React Native - 按节点ID查找元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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