フロータイプを学習していますが、わかりません。 これらのうちの1つは次のとおりです。 私はbill
状態を初期化します。null
私はAPIからフェッチし、フェッチされた請求書で状態を更新します。私は、このエラーを表示し、私はまだif(this.state.bill)
フローを使用しましたが、予期せぬフロータイプのエラー、nullの可能性のあるオブジェクト
class BillPaymentPage extends Component<*, props, *> {
state = {
bill: null,
};
componentDidMount() {
this.fetchBill(this.props.id);
}
fetchBill = async (id: string) => {
const bill = await getBillById(id, this.props.token).catch(e =>
console.log(e),
);
this.setState({ bill });
};
....
render() {
if (this.state.bill) {
const totalPayment = this.state.bill.payment
.reduce((acc, curr) => acc + curr.amount, 0)
.toFixed(3);
return (
<Card>
<PaymentForm
bill={this.state.bill}
submitPayment={this.submitPayment}
billStatus={this.state.bill.status}
/>
<h3>
<FormattedMessage id="label.total" /> :{' '}
<FormattedNumber
value={this.state.bill.total}
type="currency"
currency="tnd"
/>
</h3>
<h3>
<FormattedMessage id="label.totalPayment" /> :{' '}
<FormattedNumber
value={totalPayment}
type="currency"
currency="tnd"
/>
</h3>
<PaymentList
deletePayment={this.deletePayment}
payment={this.state.bill.payment}
/>
</Card>
);
}
return <Loader />;
}
}
:私はthis.state.bill
がnull
であれば、私はそのようなif
文の中this.state.bill
のそれので、私だけのアクセス特性に任意のプロパティにアクセスできないことを知っています:
app/components/pages/bills/BillPaymentPage.js:99
99: billStatus={this.state.bill.status}
^^^^^^ property `status`. Property cannot be accessed on possibly null value
99: billStatus={this.state.bill.status}
^^^^^^^^^^^^^^^ null
app/components/pages/bills/BillPaymentPage.js:104
104: value={this.state.bill.total}
^^^^^ property `total`. Property cannot be accessed on possibly null value
104: value={this.state.bill.total}
^^^^^^^^^^^^^^^ null
app/components/pages/bills/BillPaymentPage.js:119
119: payment={this.state.bill.payment}
^^^^^^^ property `payment`. Property cannot be accessed on possibly null value
119: payment={this.state.bill.payment}
^^^^^^^^^^^^^problem
どのように私はこのproblemeを解決することができますか?
ありがとうございますが、それでも同じエラーが解決されない問題は解決しませんでした。 –
@ziedhajsalah、今回はどんなエラーが出ますか?スクリーンショットを取得しましたか? – Chris
同じエラースクリーンショットを追加します。 –