javascript - 类型错误 : Cannot read property 'measure' of undefined

标签 javascript reactjs react-native native

我在 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”

最佳答案

  1. 您在多个组件上使用相同的引用

  2. 来自 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/

相关文章:

javascript - 使用 jquery 切换显示子项

javascript - 为什么不设置 document.domain 来允许对父域的 AJAX 请求?

javascript - Textfields组件编号验证React js

javascript - React Native 多个开关

ios - 如何在我的 iOS Native SDK 中添加 React Native 支持层

javascript - 如何使用 AngularJs 防止屏幕闪烁?

javascript - 与直接设置 css 相比,jQuery 动画开销速度为 0

javascript - 如何更新代表相同数据的两个不同的 React 组件实例?

javascript - 如何编辑 React 组件并更改屏幕上显示的文本

xcode - react native 。一个信号。未找到 'com.myapp.ios.OneSignalNotificationServiceExtension' 的配置文件