亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 componentDidMount 中設置狀態時,React 應用程序不顯示來自 api 的數據

在 componentDidMount 中設置狀態時,React 應用程序不顯示來自 api 的數據

MM們 2023-09-28 17:07:54
當從 Laravel 控制器返回響應時,例如return response()->json([    'status' => true,    'message' => 'success',    'cat_products' => $cat_products,    'cat_count' => $cat_count]);然后,實際數組可在 javascript 端的 response.data 鍵下使用。由于您在 if 語句中進行松散比較,因此它與 response.status: 200 進行比較,而不是與您"status" => true在 json 響應中提供的比較if (response.status == true) {    //response.data should contain the data sent as response from controller    console.log(response.data);         //response.cat_products.forEach(filter); //filter is a function    response.data.cat_products.forEach(filter);  //this should work}
查看完整描述

2 回答

?
喵喔喔

TA貢獻1735條經驗 獲得超5個贊

由于您返回的是一個數組而不是一個對象,因此getPagedData您需要將其更新componentDidMount為:


componentDidMount(){

  const [aircrafts, totalCount] = this.getPagedData();

  this.setState({ aircrafts, totalCount });

}


查看完整回答
反對 回復 2023-09-28
?
人到中年有點甜

TA貢獻1895條經驗 獲得超7個贊

getPageData函數中,您返回一個數組。但你正在解構一個對象

return [ aircrafts, totalCount ];

而不是這個

return { aircrafts, totalCount };


查看完整回答
反對 回復 2023-09-28
  • 2 回答
  • 0 關注
  • 156 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號