我在 native react 中遇到测量问题:
checkPositionComponant = (id) => {
this.refs.component.measure((a, b, width, height, pageX, pageY) => {
tabTag[id].positionY = pageY;
});
}
componentTag() {
return tabTag.map((item, id) => {
return(
<View key={id} style={styles.componentView} ref="component">
{ this.checkPositionComponant(id) }
</View>
);
});
}
我的错误是:TypeError:无法读取未定义的属性“measure”
最佳答案
您在多个组件上使用相同的引用
来自 react docs :
Never access refs inside of any component's render method – or while any component's render method is even running anywhere in the call stack.
您可能希望使代码与 React 的生命周期保持一致并使用
componentDidMount() {
this.checkPositionComponant(id);
}
其中 checkPositionComponant
应更改状态(setState/调度)以便组件重新渲染。
看看this example .
关于javascript - 类型错误 : Cannot read property 'measure' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46774016/