Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 09304d9

Browse files
committed
add beforeunload
1 parent f6b10a0 commit 09304d9

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

package-lock.json

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"bootstrap": "5.2.2",
1010
"http-proxy-middleware": "^2.0.6",
1111
"react": "^18.2.0",
12+
"react-beforeunload": "^2.5.3",
1213
"react-bootstrap": "^2.5.0",
1314
"react-bootstrap-icons": "1.9.1",
1415
"react-dom": "^18.2.0",

src/components/Player.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Col from 'react-bootstrap/Col';
1111
import Container from 'react-bootstrap/Container';
1212
import Row from 'react-bootstrap/Row';
1313
import { useParams } from 'react-router-dom';
14+
import { useBeforeunload } from 'react-beforeunload';
1415

1516
function Player() {
1617

@@ -126,6 +127,12 @@ function Player() {
126127
isPlaying: false,
127128
});
128129

130+
useBeforeunload((event) => {
131+
if (playerState.dirty) {
132+
event.preventDefault()
133+
}
134+
});
135+
129136
const [ error, setError ] = useState(false)
130137

131138
return (

0 commit comments

Comments
 (0)