以下のコードでは、子QRScan
コンポーネントに渡される小道具closeModal
?子コンポーネント内の関数で呼び出すことができるように、小道具を介して子コンポーネントに渡された関数にバインドするにはどうすればいいですか?
私は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();
}
}
以下を追加する必要があります! –