我正在向我的数据库发出 GET 请求,数据正在进入并显示,但是当数据在我的数据库上更新时,它不会在使用该数据的组件上更新。
编辑 根据我需要不断发出 GET 请求的评论,所以我更新了问题。
useEffect() 设置数据状态
// Data for each of the tags
const [tag1, setTag1] = useState();
const [tag2, setTag2] = useState();
const [tag3, setTag3] = useState();
useEffect(() => {
axios
.get(URL)
.then(res => {
const data = res.data;
// Passing in the setter method
setTagDetails(data, setTag1, setTag2, setTag3);
})
.catch(error => {
console.log(error);
});
}, []);
setTagDetails() 其中传入的数据被分成不同的状态以在组件中使用:
// Splitting the data by each corresponding tag and adding them to their respective state
export const setTagDetails = (data, setTag1, setTag2, setTag3) => {
const arr1 = [];
const arr2 = [];
const arr3 = [];
try {
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) {
// Split the data for the tags into their respective name
const name = d["tags"]["L"][0]["M"]["name"]["S"];
// Will be used to set individual datasets for each tag
if (name === "Tag1") {
cleanTag(d, arr1);
}
if (name === "Tag2") {
cleanTag(d, arr2);
}
if (name === "Tag3") {
cleanTag(d, arr3);
}
}
});
setTag1(arr1);
setTag2(arr2);
setTag3(arr3);
} catch (err) {
console.log(err);
}
};
qq_花开花谢_0
相关分类