第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

使用 React 在 axios 中傳遞參數(shù)時(shí)出錯(cuò)

使用 React 在 axios 中傳遞參數(shù)時(shí)出錯(cuò)

炎炎設(shè)計(jì) 2023-02-17 17:32:13
我正在嘗試通過(guò)如下傳遞某些參數(shù)來(lái)使用 Axios 進(jìn)行 GET 請(qǐng)求,const fetchData = async () => {    const response = await axios      .get(config.App_URL.getAllMock, {        params: {          customHostName: customerData,          type: "mock",        },      })      .catch((error) => {        console.error(`Error in fetching the data ${error}`);      });    let list = [response.data.routeManager];    setData(list);    setLoading(true);  };customerData從通過(guò) contextAPI 傳遞給此組件的另一個(gè)對(duì)象中獲取。 const [options, setOptions] = useContext(CustomerContext);然后它被映射如下,const hostNames = [];  options.map((name, index) => {    hostNames.push(name.customer.customHostName);  });  const customerData = hostNames[0];請(qǐng)求失敗,404因?yàn)閏ustomerData 沒(méi)有作為參數(shù)傳遞到負(fù)載中。所以當(dāng)我檢查日志 url 是這樣的時(shí),htts://abc.com/v1/route/getAllRoutes?type=mock 404我嘗試記錄 then 的值,customerData在這種情況下我可以看到要傳遞的預(yù)期值。關(guān)于如何將customHostNameas 參數(shù)傳遞到有效載荷中的任何想法?用我指的組件更新了問(wèn)題,const MainContent = () => {  const [options, setOptions] = useContext(CustomerContext);  const hostNames = [];  options.map((name, index) => {    hostNames.push(name.customer.customHostName);  });  const customerData = hostNames[0];  // Get all the mock  const fetchData = async () => {    const response = await axios      .get(config.App_URL.getAllMock, {        params: {          customHostName: customerData,          type: "mock",        },      })      .catch((error) => {        console.error(`Error in fetching the data ${error}`);      });    ....    ....    ....  };  useEffect(() => {    fetchData();  }, []);  return (    <div>     ....     ....     ....    </div>  );};export default MainContent;
查看完整描述

1 回答

?
智慧大石

TA貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超3個(gè)贊

您正在使用options從上下文中獲取的變量,如果此變量是從異步函數(shù)中獲取的,則需要通過(guò)將此變量添加到 useEffect 依賴項(xiàng)數(shù)組中來(lái)偵聽(tīng)此變量的更改。


您的版本不起作用,因?yàn)槟徽{(diào)用了一次 fetchData 函數(shù)(在初始渲染之后)。


  const fetchData = (customerData) => {

    ...

  }  


   useEffect(() => {

      if(options) {

       const hostNames = [];

       options.map((name, index) => {

       hostNames.push(name.customer.customHostName);

       });

        const customerData = hostNames[0];


        fetchData(customerData);

      } 

     }, [options]);


查看完整回答
反對(duì) 回復(fù) 2023-02-17
  • 1 回答
  • 0 關(guān)注
  • 153 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)