【发布时间】:2018-01-10 08:04:20
【问题描述】:
我有一个 ScrollView 作为我的视图的根。在其中,我展示了一堆 Text 组件。每个组件都有自己的“参考”,因此它们是可访问的。 现在,我传递了一个带有数字的道具,该数字告诉我应该滚动到哪个文本。所以我需要先测量它。我尝试了这四种方法,但它们中的每一种都只给了我未定义的值。 有什么想法吗?
componentWillReceiveProps(nextProps) {
//this is a reference to my 'Text' component
const chapterNo = nextProps.navigation.state.params.chapter;
const chapterRef = this.refs["chapter" + chapterNo];
//method 1:
var RCTUIManager = require('NativeModules').UIManager;
var handle = ReactNative.findNodeHandle(chapterRef);
RCTUIManager.measure(handle, (x, y, width, height, pageX, pageY) => {
console.log(x, y, width, height);
});
//method 2:
NativeMethodsMixin.measure.call(chapterRef, (x, y, width, height) => {
console.log(x, y, width, height);
});
//method 3:
chapterRef.measure((ox, oy, width, height, px, py) => {
console.log(ox, oy, width, height);
});
//method 4 (same as 3 but with setTimout):
setTimeout(() => {
chapterRef.measure((ox, oy, width, height, px, py) => {
console.log(ox, oy, width, height);
});
}, 0);
}
【问题讨论】:
-
您确定
chapterRef指向有效组件吗?您应该避免将字符串文字作为参考。它们已被弃用。 -
是的,这是我检查过的东西,我确信这个参考是好的,并且组件参考是有效的。我知道字符串不是最好的,但是一旦我解决了这个问题,我会在稍后更新它。事实证明,测量滚动视图中的任何内容在 RN 中都是一件令人头疼的事情......
标签: javascript react-native react-navigation