[英]React native passing variable inside a function
我有一個函數,我想在該函數中傳遞一個變量。 這個sendDraft函數將接收參數
sendDraft(billnumber) {
axios
.post("http://192.168.0.111:3000/sendDraft", {
invoice: billnumber,
})
.then((response) => {
console.log(response);
});
}
這是將參數發送到sendDraft函數的函數
let drafts = this.state.invoice.map((invc) => {
return (
<Card key={invc.invoiceId}>
<Card.Title>Invoice Number: {invc.billnumber}</Card.Title>
<Card.Divider />
<View>
<Text>
Customer's address:
{invc.address}
</Text>
<Text>Customer's email: {invc.email}</Text>
<Text>Total Price: {invc.price}</Text>
</View>
<Button //Button which will be pressed to send parameter
title="SEND DRAFT"
onPress={this.sendDraft.bind(this, invc.billnumber)}
/>
</Card>
);
});
那是按鈕(最后一個按鈕)
我確信 invc.billnumber 不為空。
但是當我單擊按鈕時,變量沒有傳遞,更新語句也找不到記錄。
(API 在郵遞員中完美運行)。
有什么問題,我們如何在 react native 中傳遞變量。
要確認 invc.billnumber 不為空或未定義,您必須調試它們。 在返回之前對其進行控制台以檢查 invc.billnumber 是否為空或未定義。
console.log(invc.billnumber);
第二個問題可能與您實現 onPress 事件的綁定有關。 您可以使用 ES6 語法在構造函數中進行綁定:
export default class Abc extends Component {
constructor(props) {
super(props);
this.sendDraft= this.sendDraft.bind(this);
}
進而:
let drafts = this.state.invoice.map((invc) => {
console.log(invc.billnumber); //To check invc.billnumber value
return (
<Card key={invc.invoiceId}>
<Card.Title>Invoice Number: {invc.billnumber}</Card.Title>
<Card.Divider />
<View>
<Text>
Customer's address:
{invc.address}
</Text>
<Text>Customer's email: {invc.email}</Text>
<Text>Total Price: {invc.price}</Text>
</View>
<Button
title="SEND DRAFT"
onPress={() => this.sendDraft(invc.billnumber)}> //Bind like this
/>
</Card>
);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.