如何在react-native中找到子组件的节点句柄?

时间:2021-04-18 18:59:03

I had setup a ViewPager component in my react-native application which goes like this:

我在我的react-native应用程序中设置了一个ViewPager组件,如下所示:

render(){
  return(
    <View>
       <ViewPager
         ref="main"
         dataSource={this.state.dataSource}
         renderPage={this.renderView}
       />
       <TouchableHighlight onPress={this._onPressButton}>
           <Text>Something</Text>
       </TouchableHighlight>     
    </View>  
  );
}

renderView(){
    return(
      <View ref="main1">
          <Text>Something else</Text>     
      </View>
    );
}

_onPressButton(){
    //Do something with {this.refs.main} and {this.refs.main1}
}

Now I want to access the node handle of View with main1 reference. I can find the node handle of ViewPager using findNodeHandle(this.refs.main) but this.refs.main1 returns null. How can find this child View's node handle?

现在我想使用main1引用访问View的节点句柄。我可以使用findNodeHandle(this.refs.main)找到ViewPager的节点句柄,但this.refs.main1返回null。怎么能找到这个子View的节点句柄?

1 个解决方案

#1


0  

I read somewhere that views which only contain one child are collapsed. It is entirely possible that that is what is happening in your case. I was able to force the view to not be collapsed by attaching a border to it. Obviously this isn't a solution, but it would at least tell you if that is what is happening.

我在某处读到只包含一个孩子的视图被折叠。这种情况完全有可能发生在您的案例中。我能够通过附加边框来强制视图不被折叠。显然这不是一个解决方案,但它至少会告诉你这是不是正在发生的事情。

#1


0  

I read somewhere that views which only contain one child are collapsed. It is entirely possible that that is what is happening in your case. I was able to force the view to not be collapsed by attaching a border to it. Obviously this isn't a solution, but it would at least tell you if that is what is happening.

我在某处读到只包含一个孩子的视图被折叠。这种情况完全有可能发生在您的案例中。我能够通过附加边框来强制视图不被折叠。显然这不是一个解决方案,但它至少会告诉你这是不是正在发生的事情。