React Native:如何聚焦包裹在自定义组件中的TextInput?

问题描述 投票:0回答:2

在我的React Native应用程序中,我有一个包装了TextInput的自定义组件。在使用自定义组件的代码中,我想在.focus()上调用TextInput。我试过像这样在自定义组件上直接调用它:

<CustomComponent
  ref={ref => {
    this.customComponent = ref;
  }}
/>

this.customComponent.focus();

但是我不能,因为我认为自定义组件没有focus()方法。我可以找到一种在包装的TextInput上调用它的方法,但是我想知道是否存在一种在自定义组件上直接调用它的方法。

有人知道我该如何处理吗?

在我的React Native应用中,我有一个包装TextInput的自定义组件。在使用自定义组件的代码中,我想在TextInput上调用.focus()。我尝试直接在自定义......>

reactjs react-native focus textinput
2个回答
0
投票

如果是类组件,则可以使用CustomComponent.prototype.yourFunction()

[youFunction使用ref调用focus()上的TextInput


0
投票

如果CustomComponent是您自己的组件,那么在这种情况下,您可以通过Forwarding Refs

© www.soinside.com 2019 - 2024. All rights reserved.