Skip to content

Commit ed710cb

Browse files
committed
fix handleVisibilityChange
1 parent f3c2617 commit ed710cb

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

admin-ui/src/pages/redux/test1.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {PageContainer} from "@ant-design/pro-components";
88
const Test1 = () => {
99
const counter = useSelector((state: RootState) => state.counter.value);
1010
const dispatch = useDispatch();
11-
1211
return (
1312
<PageContainer>
1413
<div style={{
@@ -33,4 +32,4 @@ const Test1 = () => {
3332
);
3433
}
3534

36-
export default Test1;
35+
export default Test1;

admin-ui/src/pages/redux/test2.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const Test2 = () => {
99
const counter = useSelector((state: RootState) => state.counter.value);
1010
const dispatch = useDispatch();
1111

12-
1312
return (
1413
<PageContainer>
1514
<div style={{
@@ -35,4 +34,4 @@ const Test2 = () => {
3534
);
3635
}
3736

38-
export default Test2;
37+
export default Test2;

admin-ui/src/pages/welcome/index.tsx

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, {useEffect} from 'react';
22
import logo from '@/assets/logo.svg';
33
import './index.scss';
44
import {useSelector} from "react-redux";
@@ -11,6 +11,26 @@ const Index = () => {
1111
const counter = useSelector((state: RootState) => state.counter.value);
1212
const username = localStorage.getItem('username');
1313

14+
useEffect(() => {
15+
const handleVisibilityChange = () => {
16+
if (document.visibilityState === 'visible') {
17+
console.log('Tab is active');
18+
// 执行页面重新激活时的操作
19+
} else {
20+
console.log('Tab is inactive');
21+
// 执行页面被隐藏时的操作
22+
}
23+
};
24+
25+
// 监听 visibilitychange 事件
26+
document.addEventListener('visibilitychange', handleVisibilityChange);
27+
28+
// 清理事件监听器
29+
return () => {
30+
document.removeEventListener('visibilitychange', handleVisibilityChange);
31+
};
32+
}, []);
33+
1434
return (
1535
<Page enablePageContainer={true}>
1636
<div className="App">

0 commit comments

Comments
 (0)