【发布时间】:2022-01-14 14:13:47
【问题描述】:
首先,请看代码。
const [currentUserPK, setCurrentUserPK] = useState(undefined);
const [currentPage, setCurrentPage] = useState(1);
const [rowsPerPage, setRowsPerPage] = useState(10);
//현재 USER_PK 가져오는 API
const getCurrentUserPk = async () => {
const url = '/api/account/decoding-token';
const res = await get(url)
.then((res) => {
console.log(res);
setCurrentUserPK(res.data.USER_PK);
})
.catch((error) => {
console.log(error);
});
};
useEffect(() => {
getCurrentUserPk()
},[]);
//생성된 액션 아이템 불러오기 API
const getActionItems = async () => {
const url = `/api/work/view-items`;
const params = {
id: currentUserPK,
currentPage: currentPage,
feedsPerPage: rowsPerPage,
};
await get(url, { params: params }).then((res) => {
setActionItemArray(res.items);
console.log(res.items);
console.log(actionItemArray);
});
};
useEffect(() => {
getActionItems();
}, [currentPage, rowsPerPage, actionItemArray]);
以下代码会出现问题。
useEffect(() => {
getActionItems();
}, [currentPage, rowsPerPage, actionItemArray]);
当我在第二个参数数组中添加 actionItemArray 时,它一直在循环
console.log(res.items);
console.log(actionItemArray);
这两个 console.log 事件。
当我从 useEffect Hook 的第二个参数中删除 actionItemArray 时,我必须刷新页面以添加、删除和编辑 actionItems。
我不知道为什么会这样。请帮忙!
【问题讨论】:
-
问题本质上是您发现的问题。当
actionItemArray更改时,您重新渲染useEffect,并且每次调用useEffect时都会更改它 -
@Giacomo 是的,但是在我删除项目或编辑项目后,应该没有重新渲染,因为不再有任何更改。但它一直存在!!!
标签: javascript reactjs loops get