简体   繁体   English

如何修复“TypeError: undefined is not an object”?

[英]How can I fix 'TypeError: undefined is not an object'?

Here I'm trying to update index when I'm swiping:在这里,我尝试在滑动时更新索引:

export default class App extends PureComponent {
  static propTypes = {
      onIndexChanged: PropTypes.func,
      index: PropTypes.number,
  };

  index = 0;

  constructor(props) {
      super(props);
      const { index } = this.props;

      this.index = index;
  }

  ref = (el) => {
      this.swiper = el;
  };

  onIndexChanged = (newIndex) => {
      const { onIndexChanged } = this.props;
      this.index = newIndex;

      if (onIndexChanged) {
          onIndexChanged(newIndex);
      }
  };

  componentWillReceiveProps({ index }) {
      if (index !== this.index) {
          const difference = index - this.index;
          this.swiper.scrollBy(difference);
      }
  }

  render() {

      return (
          <RNSwiper
              {...this.props}
              ref={el => this.ref(el)}
              onIndexChanged={this.onIndexChanged}
          />
      );
  }
}

But I'm getting this: TypeError: undefined is not an object (evaluating '_this.state.children[_this.state.index]').但我得到了这个:TypeError: undefined is not an object (evaluating '_this.state.children[_this.state.index]')。 How can I fix that?我该如何解决?

在从对象中获取一些值之前,请先检查 null 或 undefined,请参考下面的代码,它可能会解决您的问题。

_this.state.children && _this.state.children[_this.state.index]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 整数到字符串,我该如何解决以下错误:“TypeError: Can&#39;t convert undefined to object”? - Integer to String, How can I fix the following error: "TypeError: Can't convert undefined to object"? 我该如何修复此错误错误类型错误:未定义不是 object(正在评估“userData.username”) - How can i fix this error ERROR TypeError: undefined is not an object (evaluating 'userData.username') 如何修复TypeError:undefined不是对象错误? - How to fix TypeError: undefined is not an object error? 如何在导入期间修复“ TypeError:未定义不是对象” - How to fix 'TypeError: undefined is not an object' during import 如何修复“TypeError:_this is undefined” - How to fix “TypeError: _this is undefined” 如何修复 JavaScript 中未定义的 TypeError? 对象的所有字段都存在 - How do I fix TypeError undefined in JavaScript? All fields exist for the object 如何修复 TypeError: undefined is not an object(评估“state.routes”) - How to fix TypeError: undefined is not an object (evaluating 'state.routes') 我该如何解决:未捕获的TypeError:无法读取JavaScript中未定义的属性&#39;toString&#39; - how can i fix this: Uncaught TypeError: Cannot read property 'toString' of undefined in javascript 我该如何修复TypeError:无法读取未定义REACT的属性“ map” - How can i fix TypeError: Cannot read property 'map' of undefined REACT 我该如何解决-未捕获的TypeError:无法读取未定义的属性&#39;scrollHeight&#39; - How can I fix - Uncaught TypeError: Cannot read property 'scrollHeight' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM