Why you should clean up react effects using fetch

  Рет қаралды 10,572

Web Dev Cody

Web Dev Cody

Күн бұрын

Пікірлер: 32
@ΧαρηςΧανοζιδης
@ΧαρηςΧανοζιδης 2 жыл бұрын
We learning boys
@rrthurein4990
@rrthurein4990 2 жыл бұрын
Gang gang
@szabolcsjobbagy30
@szabolcsjobbagy30 2 жыл бұрын
It's very nice that besides showing the code (even 2 or more alternative solutions of the same problem) you tell us the reasons as well, the concept behind it, why are these good/bad practices, we can learn a lot from you, Thank You!!
@blovio
@blovio 2 жыл бұрын
Man I could've used this video like 2 months ago, trying to find good info on this was annoying... great simple explanation.
@nemanjavucic6653
@nemanjavucic6653 2 жыл бұрын
Firstly, I hit like and then start to watch
@WebDevCody
@WebDevCody 2 жыл бұрын
Yeah buddyy
@JasonLayton
@JasonLayton 10 ай бұрын
This is just what I needed. Thank you.
@lukor-tech
@lukor-tech 2 жыл бұрын
This is something I needed and I didn't know I did.
@dcknature
@dcknature 2 жыл бұрын
Thanks for showing us the proper way to handle this kind of situations 👍! Some apps are infamous for doing it the lazy way, plus confusing their users with ambiguous messages like: "No connection!", when the user's connection is absolutely fine 😁.
@amershboul9107
@amershboul9107 2 жыл бұрын
💡you can do it like that useEffect(() => { let mounted = true ( async () => { const response = await fetchData(); if ( response.data && mounted ) { setData(response.data) } })() return () => { mounted = false } },[])
@MrRe-sj2iv
@MrRe-sj2iv Жыл бұрын
I think aborting a Promise is more optimize than go around it by using mounted check.
@SahraClayton
@SahraClayton 2 жыл бұрын
Another great video.
@adarsh-chakraborty
@adarsh-chakraborty 2 жыл бұрын
I like these short informative videos
@tahielgomez
@tahielgomez Жыл бұрын
really helpfull. thanks!!
@StiflerNR
@StiflerNR 2 жыл бұрын
Would something like this work with React + Redux + Redux-Saga + Service layer (built on open-api generator) ? In useEffect im dispatching action which go to saga => saga calls a service => service returns data and set them to ReduxStore Something like i generate AxiosCancelationToken in UseEffect --> send it to saga -> map it to service.method({ options: { cancelationToken: tokenFromUseEffect }}) and then in cleanUp function in useffect i trigger cancelationToken (Axiox.CancelToken.source().cancel()) Is this correct way ?
@WebDevCody
@WebDevCody 2 жыл бұрын
I think so, I’d probably send the signal from the component inside the dispatch payload maybe so that when you unmount you can abort the signal
@aishanstha
@aishanstha 2 жыл бұрын
useEffect is called after each render and when setState is used inside of it, it will cause the component to re-render which will call useEffect and so on and so on.
@blovio
@blovio 2 жыл бұрын
Not if you set the dependency array (second argument of useEffect)
@rohanshahi8741
@rohanshahi8741 2 жыл бұрын
If you pass empty dependency array then it only runs on mount. And mounting and re-render is different thing.
@akilakavisinghe7189
@akilakavisinghe7189 2 ай бұрын
This actually doesn't currently work if you are using Next.js server actions to fetch API requests. Once a request is sent to the server the client can no longer abort it for some reason.
@niklassoderberg2168
@niklassoderberg2168 2 жыл бұрын
Great! (video)
@Harish-rz4gv
@Harish-rz4gv 2 жыл бұрын
Where can I find you classroom app
@raphaelgorbach1476
@raphaelgorbach1476 2 жыл бұрын
Hi reletivaly new to react, how do I do this when I call a function in useEffect that does the fetching for me? Don't I then have to pass the signal as an argument so I have an object reference to that abort controller? Also woudn't this be consider bad practice since the called function would then be impure?
@WebDevCody
@WebDevCody 2 жыл бұрын
If you’ve abstracted your api call out into a separate function then maybe have that function return a cancel method and the promise, that would allow your component to cancel your request at anytime and not be coupled to the api request details
@SeibertSwirl
@SeibertSwirl 2 жыл бұрын
Good job babe!!!!!
@WebDevCody
@WebDevCody 2 жыл бұрын
Thank my love!
@_Sonato
@_Sonato Жыл бұрын
I really think you should get into the habit of using guard clauses instead of these single line if/else statements.
@vnm_8945
@vnm_8945 2 жыл бұрын
very nice, keep up with the good work, we' learnin' here. :D
@WebDevCody
@WebDevCody 2 жыл бұрын
I’m learning too 😂
@vnm_8945
@vnm_8945 2 жыл бұрын
@@WebDevCody even better 😂
@orlvni
@orlvni 2 жыл бұрын
AbortController is more reliable cause it prevents unnecessary fetching
Why I avoid react's uncontrolled inputs
13:06
Web Dev Cody
Рет қаралды 15 М.
You should Avoid these React useEffect Mistakes
15:53
CoderOne
Рет қаралды 41 М.
One day.. 🙌
00:33
Celine Dept
Рет қаралды 78 МЛН
“Don’t stop the chances.”
00:44
ISSEI / いっせい
Рет қаралды 38 МЛН
How React Query Won
34:52
Theo - t3․gg
Рет қаралды 87 М.
Next Generation TypeScript | Prime Reacts
24:45
ThePrimeTime
Рет қаралды 104 М.
The Power of AbortController in JavaScript
17:56
Software Developer Diaries
Рет қаралды 9 М.
Why I always use react-query on my react apps
14:09
Web Dev Cody
Рет қаралды 30 М.
Goodbye, useEffect - David Khourshid
29:59
BeJS
Рет қаралды 503 М.
This React challenge is fair game for a junior web dev interview
23:07
Why Signals Are Better Than React Hooks
16:30
Web Dev Simplified
Рет қаралды 491 М.
Debugging memory leaks - HTTP 203
22:04
Chrome for Developers
Рет қаралды 47 М.
Abort Fetch API Requests using AbortController
6:37
Colby Fayock
Рет қаралды 12 М.