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

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

在 React 中從 API 訪問(wèn)數(shù)據(jù)時(shí)重新呈現(xiàn)太多

在 React 中從 API 訪問(wèn)數(shù)據(jù)時(shí)重新呈現(xiàn)太多

POPMUISE 2023-05-25 17:18:44
我正在向鏈接到我的數(shù)據(jù)庫(kù)的 API 發(fā)出獲取請(qǐng)求。dataApi 是一個(gè)非常大的對(duì)象,其中嵌套了很多對(duì)象和數(shù)組。數(shù)據(jù)庫(kù)中的某些條目沒(méi)有我需要的完整詳細(xì)信息,因此我過(guò)濾它們以僅顯示長(zhǎng)度大于 5 的條目。現(xiàn)在的問(wèn)題是,當(dāng)我嘗試獲取每個(gè)條目的名稱(chēng)時(shí),這些條目被拆分為T(mén)ag1, Tag2 or Tag3.在此之前,當(dāng)我訪問(wèn)所有條目并獲取其中的項(xiàng)目時(shí),沒(méi)有問(wèn)題。但是,當(dāng)我嘗試按名稱(chēng)過(guò)濾它們并將與該名稱(chēng)對(duì)應(yīng)的對(duì)象存儲(chǔ)在其狀態(tài)時(shí),就會(huì)出現(xiàn)此問(wèn)題。編輯: 當(dāng)我console.log(arr1)顯示所有數(shù)據(jù)時(shí),但當(dāng)我將狀態(tài)設(shè)置為它時(shí),它會(huì)導(dǎo)致錯(cuò)誤。// Data from all entries in databaseconst [dataApi, setDataApi] = useState();// Data for each of the tagsconst [tag1, setTag1] = useState();const [tag2, setTag2] = useState();const [tag3, setTag3] = useState();useEffect(() => {  axios.get(URL).then((res) => {    const data = res.data;    setDataApi(data);  });}, []);const getTagDetails = data => {    const arr1 = [];    const arr2 = [];    const arr3 = [];    data &&        data.forEach(d => {            // Entries into the database which do not have any tag information            // have a size of 5 and those with all the details have a size of 6            const sizeOfObject = Object.keys(d).length;            // Only need database items with all the details            if (sizeOfObject > 5) {                const name = d["tags"]["L"][0]["M"]["name"]["S"];                // Split the data for the tags into their respective name                // Will be used to set individual datasets for each tag                if (name === "Tag1") {                    arr1.push(d);                }                if (name === "Tag2") {                    arr2.push(d);                }                if (name === "Tag3") {                    arr3.push(d);                }            }        });    setTag1(arr1);    setTag2(arr2);    setTag3(arr3);};getTagDetails(dataApi);
查看完整描述

2 回答

?
慕桂英3389331

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

我想問(wèn)題是你在文件內(nèi)部調(diào)用getTagDetails(dataApi)所以它導(dǎo)致了這個(gè)無(wú)限渲染問(wèn)題


而是刪除 getTagDetails 并嘗試在 API 解析后調(diào)用此函數(shù)。


useEffect(() => {

  axios.get(URL).then((res) => {

    const data = res.data;

    getTagDetails(data)

  });

}, []);


查看完整回答
反對(duì) 回復(fù) 2023-05-25
?
森林海

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

我認(rèn)為您的問(wèn)題是您構(gòu)建功能的方式getTagDetails。每次渲染時(shí),您都會(huì)調(diào)用getTagDetails(),您做的第一件事就是為每個(gè)標(biāo)簽創(chuàng)建一個(gè)新數(shù)組。當(dāng)您使用setTag新數(shù)組調(diào)用時(shí),它將重新呈現(xiàn)。您可能希望將getTagDetails邏輯移動(dòng)到效果中,以便它只在掛載時(shí)運(yùn)行一次(或者如果您需要更新新數(shù)據(jù),則向依賴項(xiàng)數(shù)組添加依賴項(xiàng))



查看完整回答
反對(duì) 回復(fù) 2023-05-25
  • 2 回答
  • 0 關(guān)注
  • 162 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(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)