如何在 react-native android 中 findViewById()

如何在 react-native android 中 findViewById()

问题描述:

我在 index.android.js 文件中提到了这个..

I'm referring to this in my index.android.js file..

<View>
  <CustomView>
    ...
  </CustomView>
</View>

这可以通过 React.findNodeHandle 来完成.

This can be done by React.findNodeHandle.

就你而言,

...

var { findNodeHandle } = React;

...

render: function() {
  return (
    <View>
      <CustomView ref="customView">
        ...
      </CustomView>
    </View>
  );
},

somethingLikeComponentDidMount: function() {
  var customViewNativeID = findNodeHandle(this.refs.customView);
  // Something else...
}

...

可能会做这项工作.

有关工作示例(本机绑定两个组件),请查看 此处 作为 JS 部分和 这里 作为原生 Java 部分.

For a working example (natively binding two components), check out here as the JS part and here as the native Java part.