在学习RN(react native)的过程中遇到红屏错误是 TypeError:Cannot read property 'state' of undefined 说没有state这个属性,我就很蒙了。
贴出错误代码
_startAnimation({show}) {
let handler = this.props.startCustomAnimation || this._startDefaultAnimation;
handler({show, doneCallback: () => this.setState({isTransitioning: false})});
this.setState({isTransitioning: true});
}
_startDefaultAnimation({show, doneCallback}) {
var animDuration = 300;
var values = this.state.defaultAnimatedValues;//报错 this = undefined
}
最后百度了一下发现 handler
要加个 bind(this)
来调用作用域才对 这边给个正确调用代码
handler.bind(this)({show, doneCallback: () => this.setState({isTransitioning: false})});