我正在向鏈接到我的數據庫的 API 發出獲取請求。dataApi 是一個非常大的對象,其中嵌套了很多對象和數組。數據庫中的某些條目沒有我需要的完整詳細信息,因此我過濾它們以僅顯示長度大于 5 的條目?,F在的問題是,當我嘗試獲取每個條目的名稱時,這些條目被拆分為Tag1, Tag2 or Tag3.在此之前,當我訪問所有條目并獲取其中的項目時,沒有問題。但是,當我嘗試按名稱過濾它們并將與該名稱對應的對象存儲在其狀態時,就會出現此問題。編輯: 當我console.log(arr1)顯示所有數據時,但當我將狀態設置為它時,它會導致錯誤。// 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);
在 React 中從 API 訪問數據時重新呈現太多
POPMUISE
2023-05-25 17:18:44