我遇到一個問題,其中this
在我的商店中爲我的React組件返回null
。我相信我需要對此加以約束,但我不確定。我也在使用React Router,並將我的組件設置在一個包裝中,這樣我就可以將道具傳遞給它。任何幫助表示讚賞!React Store對`this`的值返回null
COMPONENT
import React from 'react';
import PaymentStore from './../store/paymentStore';
class InitialPaymentScreen extends React.Component {
constructor(props) {
super(props)
}
render() {
console.log(this.props)
return (
<div className="payment-form-submit" onClick={this.props.store.init}>Next</div>
);
}
}
class PaymentForm extends React.Component {
constructor(props) {
super(props)
}
render() {
return (
<section className="main-content payment-form-wrapper">
<InitialPaymentScreen store={this.props.store}/>
</section>
);
}
}
export default class PaymentFormWrapper extends React.Component {
render() {
return (
<PaymentForm store={PaymentStore} mode="foo"/>
);
}
}
STORE
let PaymentStore = {
handleClickNext() {
console.log("hello")
},
init() {
console.log(this) // returns null
this.handleClickNext(); // cannot read property of null
},
};
export default PaymentStore;
太棒了,就像一個魅力。你能否澄清爲什麼我必須把這個綁定? – u111937
當然,我剛剛更新了一些背景的答案! –