Warn Users When Leaving a Page in React with beforeunload

  Рет қаралды 15,663

Colby Fayock

Colby Fayock

Күн бұрын

Prevent navigation or page refreshing with unsaved changed using the beforeunload event in a React app.
We'll walk through setting up the beforeunload event listener inside of a useEffect hook and watching for changes in the form to dynamically apply the listener only when needed.
Dig deeper into Full Stack React with my upcoming Appwrite course: spacejelly.dev...
📺 KZbin: kzbin.info...
🐦 Twitter: / colbyfayock
📹 Twitch: / colbyfayock
✉️ Newsletter: www.colbyfayoc...
🎥 What I Use: www.colbyfayoc...
#colbyfayock #reactjs #webdevelopment

Пікірлер: 51
@colbyfayock
@colbyfayock Ай бұрын
Learn how to build a full stack Next.js app in my upcoming course: colbyfayock.com/course
@jawyor-k3t
@jawyor-k3t Жыл бұрын
Every time I check in, you have added some unique and interesting project videos. You explain things very clearly and talk smoothly. Great job dude
@colbyfayock
@colbyfayock Жыл бұрын
thank you!!
@adamjamiu6764
@adamjamiu6764 Жыл бұрын
I swear to God. I have been searching the whole internet for this functionality. Big Gracias to you fayock❤
@colbyfayock
@colbyfayock Жыл бұрын
glad this made it over to you!! 🙌 no problem
@BjornBrasse
@BjornBrasse Жыл бұрын
Again a great video, very clearly explained, thanks!!! 😊
@colbyfayock
@colbyfayock Жыл бұрын
no problem!!
@chandanthakur996
@chandanthakur996 9 ай бұрын
Great explanation Colby. Thanks a lot!
@colbyfayock
@colbyfayock 9 ай бұрын
no problem!
@urosmilosavljevic5082
@urosmilosavljevic5082 11 ай бұрын
This works for reloading or closing the tab, but how do you handle the case when user uses in-app navigation like links?
@colbyfayock
@colbyfayock 11 ай бұрын
assuming you're using a router, you could watch for route change, though that wouldn't be _before_ the page leaves, that would be technically after. you'd have to be able to hook into the routing lifecycle somehow
@jamildanielsson3236
@jamildanielsson3236 9 ай бұрын
@urosmilosavljevic5082 Did you find a solution for this? I am having the same problem
@pavelstraka9960
@pavelstraka9960 9 ай бұрын
@@jamildanielsson3236 I have the same issue, I need to detect the in app change
@mageshmagi6167
@mageshmagi6167 2 ай бұрын
@@jamildanielsson3236 same problem here any solution ?
@user-jr5go9ye5o
@user-jr5go9ye5o 23 күн бұрын
I wish you would show the whole code, its difficult to follow with half the screen of the actual code cut off
@colbyfayock
@colbyfayock 23 күн бұрын
sorry about that, trying to get better at hiding the sidebar and a little less zoomed in when working through the code
@thanglong9549
@thanglong9549 8 ай бұрын
In the case of using useNavigate of react-router-dom v6 to redirect when clicking on a button, it cannot prevent navigation.
@colbyfayock
@colbyfayock 8 ай бұрын
im not familiar with that API but i suspect the JS used for navigation there isn't triggering the beforeunload event
@codewithyunus8423
@codewithyunus8423 Жыл бұрын
hey Colby Fayock nice explanation
@colbyfayock
@colbyfayock Жыл бұрын
Thank you!
@user-sm4cs6un2z
@user-sm4cs6un2z Жыл бұрын
how can i add a custom alert message on screen when reloading, rather than showing the inbuilt browser alert like u did ?
@colbyfayock
@colbyfayock Жыл бұрын
unfortunately my understanding is that they removed the ability because people were abusing that (of course they were), so i dont think you're able to
@user-sm4cs6un2z
@user-sm4cs6un2z Жыл бұрын
@@colbyfayock ok thnx
@user-xm2ge3pm3t
@user-xm2ge3pm3t 4 ай бұрын
this is only working for refresh , if I am clicking on different navItems in navbar, when Route changes its not working. Is there any other function for that
@colbyfayock
@colbyfayock 4 ай бұрын
not that im aware of, you can try to detect the mouse movement to make a guess that they're going to switch tabs, but dont believe there's a function. switching tabs doesnt close the tab, hence why it wouldnt fire
@vendjin
@vendjin 3 ай бұрын
Tell me, does this work in such a case? If the tab is closed, send a request to the API, and if you reload the page, then no action is required to send to the API
@colbyfayock
@colbyfayock 3 ай бұрын
if im undersatnding your scenario correctly, if the user closes the tab, you're not able to then make a new API request. this is a mechnanism to provide a warning if someone tries to close the tab if there are changes on the page or potentially in the middle of an API request that already exists, but haven't tested that
@user-ck8px9pp4x
@user-ck8px9pp4x 3 ай бұрын
What if my site has links to another pages?
@kumart2025
@kumart2025 8 ай бұрын
Can you prvide any useful resource for react 0.14 and react-route 2.2?
@suwetaaramesh1867
@suwetaaramesh1867 6 ай бұрын
@colbyfayock but this happens only in homepage. Could you tell me how to trigger this in subpages?
@colbyfayock
@colbyfayock 6 ай бұрын
do you mean after navigating to another page clientside? what router are you using?
@brijspy
@brijspy 6 ай бұрын
can we change the text to have our own text inside the dialog that pops up ?
@colbyfayock
@colbyfayock 6 ай бұрын
unfortunately i dont believe you're currently able to customize that
@jac4020
@jac4020 3 ай бұрын
How do you hand both page refresh and browser back button? Thanks
@colbyfayock
@colbyfayock 3 ай бұрын
hey can you elaborate on what you mean? can you give me an example?
@CetateanulDinFiguraAlaturata
@CetateanulDinFiguraAlaturata 3 ай бұрын
@@colbyfayock I think he means to ask if browser back button is covered by this? If not, how do you do it?
@colbyfayock
@colbyfayock 3 ай бұрын
@@CetateanulDinFiguraAlaturata the video shows it working for refresh. it should work with back button with the exception that apparently firefox doesn't support the back/forward button cache and may depend on how you're providing routing in the application, particularly if you're not properly updating the browser state
@mocastello9253
@mocastello9253 3 ай бұрын
great video! thankks alot ❤
@colbyfayock
@colbyfayock 3 ай бұрын
no problem!
@jerahmeelsalayog1720
@jerahmeelsalayog1720 11 ай бұрын
how can I use custom modal instead of default prompt?
@colbyfayock
@colbyfayock 11 ай бұрын
This API doesn't support custom modals due to people trying to abuse the functionality for what I assume was things like spam
@jerahmeelsalayog1720
@jerahmeelsalayog1720 11 ай бұрын
@@colbyfayock Thanks for the info!
@AbuTaher-vx2oe
@AbuTaher-vx2oe 8 ай бұрын
but it is not working my case
@colbyfayock
@colbyfayock 8 ай бұрын
what issue did you run into?
@AbuTaher-vx2oe
@AbuTaher-vx2oe 8 ай бұрын
actually., im using wavesurfer for audio visualization on vite reactjs. before move another route need to wavesurfer destroy. but if i use return on useEffect it is not working. if destroy function use button onClick then it is working. but my main issue when user click on browser back/left arrow/button or browser forward/right arrow/button still need to destroy
@AbuTaher-vx2oe
@AbuTaher-vx2oe 8 ай бұрын
@@colbyfayock (what issue did you run into?) i'm using recatjs vite. it is working sometimes after leaving from route
@colbyfayock
@colbyfayock 8 ай бұрын
@@AbuTaher-vx2oe this solution isn't using useEffect to achieve the desired effect, it only uses it for setting up the event listener for the browser event. once the event occurs, the browser specifically doesn't allow simply anything to be triggered because of the potential for abuse, so you can't really perform custom functionality like it appears you're intending to do from my understanding of the PAI
@noicehockey9920
@noicehockey9920 3 ай бұрын
Thanks.    A lot.
@colbyfayock
@colbyfayock 3 ай бұрын
no problem!
Abort Fetch API Requests using AbortController
6:37
Colby Fayock
Рет қаралды 10 М.
He bought this so I can drive too🥹😭 #tiktok #elsarca
00:22
Elsa Arca
Рет қаралды 44 МЛН
Whoa
01:00
Justin Flom
Рет қаралды 55 МЛН
WILL IT BURST?
00:31
Natan por Aí
Рет қаралды 42 МЛН
5 React Native Tips to WOW Your Users
16:36
Simon Grimm
Рет қаралды 22 М.
Upload Files in React - Typescript, Drag and Drop, & Form Examples
12:21
SEO for Developers in 100 Seconds
11:52
Fireship
Рет қаралды 608 М.
This is Why I no longer useState in Favor of URL in React
13:42
pushState and popstate
12:02
GeekLaunch
Рет қаралды 46 М.
Download Progress Bar in React with Fetch API
19:38
Colby Fayock
Рет қаралды 11 М.
Don't Use React Context!! Use This instead
13:34
CoderOne
Рет қаралды 28 М.
Save State to LocalStorage & Persist on Refresh with React.js
13:33
He bought this so I can drive too🥹😭 #tiktok #elsarca
00:22
Elsa Arca
Рет қаралды 44 МЛН