在下面的代码中,有人可以建议如何访问传递给子QRScan组件的closeModal prop中存储的函数吗?
我尝试过在QRScan的构造函数中使用this.props.closeModal.bind(this),但是当我在_onBarCodeRead函数中调用this.props.closeModal()时,我得到了一个undefined is not an object (evaluating 'this.props.closedModal')错误。
道具肯定会被传递给构造函数,我只是看起来不能正确地绑定函数。任何帮助都非常感谢!
class Test extends React.Component {
constructor(props) {
super(props);
}
_test(){
console.log("test worked")
}
render() {
return (
<View>
<QRScan closeModal={this._test}/>
</View>
}
}子类:
class QRScan extends React.Component {
constructor(props)
{
super(props)
console.log(this.props)
this.props.closeModal.bind(this);
}
render() {
return (
<BarcodeScanner
onBarCodeRead={this._onBarCodeRead}
width={windowWidth}
height={windowWidth * cameraAspectRatio}
style={styles.camera}>
<View style={styles.rectangleContainer}>
<View style={styles.rectangle} />
</View>
</BarcodeScanner>
);
}
_onBarCodeRead(e) {
console.log(e);
Vibration.vibrate();
this.props.closeModal();
}
}发布于 2016-09-23 14:54:12
问题实际上是this._onBarCodeRead没有正确绑定。
在子组件的构造函数中,您需要添加以下this._onBarCodeRead = this._onBarCodeRead.bind(this);
https://stackoverflow.com/questions/39648753
复制相似问题