Skip to content

Commit

Permalink
fix: fix none selected tab problem and improve some to do not mutate …
Browse files Browse the repository at this point in the history
…state directly
  • Loading branch information
wuxianquan authored and d3george committed Apr 12, 2024
1 parent 15cb730 commit 980ba7b
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/hooks/web/use-keepalive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@ export default function useKeepAlive() {
*/
const closeTab = useCallback(
(path = activeTabRoutePath) => {
if (tabs.length === 1) return;
const deleteTabIndex = tabs.findIndex((item) => item.key === path);
const tempTabs = [...tabs];
if (tempTabs.length === 1) return;
const deleteTabIndex = tempTabs.findIndex((item) => item.key === path);
if (deleteTabIndex > 0) {
push(tabs[deleteTabIndex - 1].key);
push(tempTabs[deleteTabIndex - 1].key);
} else {
push(tabs[deleteTabIndex + 1].key);
push(tempTabs[deleteTabIndex + 1].key);
}

tabs.splice(deleteTabIndex, 1);
setTabs([...tabs]);
tempTabs.splice(deleteTabIndex, 1);
setTabs(tempTabs);
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[activeTabRoutePath],
Expand Down Expand Up @@ -109,6 +110,8 @@ export default function useKeepAlive() {
);

useEffect(() => {
setTabs((prev) => prev.filter((item) => !item.hideTab));

if (!currentRouteMeta) return;
let { key } = currentRouteMeta;
const { outlet: children, params = {} } = currentRouteMeta;
Expand All @@ -118,10 +121,6 @@ export default function useKeepAlive() {
}
const existed = tabs.find((item) => item.key === key);

tabs.forEach((item, i) => {
item.hideTab && tabs.splice(i, 1);
});

if (!existed) {
setTabs((prev) => [
...prev,
Expand Down

0 comments on commit 980ba7b

Please sign in to comment.