[英]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.