Why you should clean up react effects using fetch

💬 Discord / discord
🤑 Patreon / webdevjunkie
🔔 Newsletter eepurl.com/hnderP
📁. GitHub github.com/codyseibert/youtube
My VSCode Extensions:
- theme: material community high contrast
- fonts: Menlo, Monaco, 'Courier New', monospace
- errors: Error Lens
- extra git help: Git Lens
- tailwind css intellisense
- indent rainbow
- material icon theme
- prettier & eslint
- ES7+ React Snippets

Пікірлер: 31

  • @user-ls5pg2wd4i
    @user-ls5pg2wd4i Жыл бұрын

    We learning boys

  • @rrthurein4990

    @rrthurein4990

    Жыл бұрын

    Gang gang

  • @szabolcsjobbagy30
    @szabolcsjobbagy30 Жыл бұрын

    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!!

  • @jacktrusler6506
    @jacktrusler6506 Жыл бұрын

    Man I could've used this video like 2 months ago, trying to find good info on this was annoying... great simple explanation.

  • @WrestlingTournamentsDotCom
    @WrestlingTournamentsDotCom4 ай бұрын

    This is just what I needed. Thank you.

  • @nemanjavucic6653
    @nemanjavucic6653 Жыл бұрын

    Firstly, I hit like and then start to watch

  • @WebDevCody

    @WebDevCody

    Жыл бұрын

    Yeah buddyy

  • @dcknature
    @dcknature Жыл бұрын

    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 Жыл бұрын

    💡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.

  • @lukor-tech
    @lukor-tech Жыл бұрын

    This is something I needed and I didn't know I did.

  • @SahraClayton
    @SahraClayton Жыл бұрын

    Another great video.

  • @adarsh-chakraborty
    @adarsh-chakraborty Жыл бұрын

    I like these short informative videos

  • @tahielgomez
    @tahielgomez8 ай бұрын

    really helpfull. thanks!!

  • @raphaelgorbach1476
    @raphaelgorbach1476 Жыл бұрын

    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

    Жыл бұрын

    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

  • @Harish-rz4gv
    @Harish-rz4gv Жыл бұрын

    Where can I find you classroom app

  • @StiflerNR
    @StiflerNR Жыл бұрын

    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

    Жыл бұрын

    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

  • @SeibertSwirl
    @SeibertSwirl Жыл бұрын

    Good job babe!!!!!

  • @WebDevCody

    @WebDevCody

    Жыл бұрын

    Thank my love!

  • @niklassoderberg2168
    @niklassoderberg2168 Жыл бұрын

    Great! (video)

  • @aishanstha
    @aishanstha Жыл бұрын

    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.

  • @jacktrusler6506

    @jacktrusler6506

    Жыл бұрын

    Not if you set the dependency array (second argument of useEffect)

  • @rohanshahi8741

    @rohanshahi8741

    Жыл бұрын

    If you pass empty dependency array then it only runs on mount. And mounting and re-render is different thing.

  • @_Sonato
    @_Sonato6 ай бұрын

    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 Жыл бұрын

    very nice, keep up with the good work, we' learnin' here. :D

  • @WebDevCody

    @WebDevCody

    Жыл бұрын

    I’m learning too 😂

  • @vnm_8945

    @vnm_8945

    Жыл бұрын

    @@WebDevCody even better 😂

  • @orlvni
    @orlvni Жыл бұрын

    AbortController is more reliable cause it prevents unnecessary fetching