The simple way to handle requests in React

  Рет қаралды 22,597

Cosden Solutions

Cosden Solutions

Күн бұрын

Check out the repo → github.com/cosdensolutions/co...
Join The Discord! → discord.cosdensolutions.io
VSCode Theme | Font → Material Theme Darker | Menlo, Monaco "monospace"
In this video I show you a simple way to handle requests in React if you're not using or you can't use something like React Query. This is a simple wrapper around Axios to create a predictable way to handle success and error responses, as well as fetch any data in React. This works with server components or client components too and it's a great alternative to consider. As a bonus, we also get to play with generic types in Typescript!

Пікірлер: 68
@jaycodes8790
@jaycodes8790 6 ай бұрын
This is why i loved your content. As you teach the very fundamental things which one need in their programming journey.
@maxilkiv
@maxilkiv 6 ай бұрын
8:37 - I think you could add at this step a check if e is instanceof AxiosError class, then it would get properly inferred
@Shaheer-xs5os
@Shaheer-xs5os 6 ай бұрын
Great content as always!
@Lykkos29
@Lykkos29 6 ай бұрын
I love that you do all of that in typescript, everything is more understandable
@physicstom4798
@physicstom4798 5 ай бұрын
Best video ever I've seen. thank you, Cosden :)
@abhinavdhama3014
@abhinavdhama3014 6 ай бұрын
Great content as always♥️. We need react interview part 2 also
@gabegabegabe573
@gabegabegabe573 5 ай бұрын
wow so nice, did not know that, the way i was doing my full-stack apliccations was api(controller/service/model) > lib folder with the fetchs > calling the functions from lib folder in my components, i was handling the errors cases in the api mostly, but with a request handler it seems way more organized
@meshinazol7267
@meshinazol7267 6 ай бұрын
Very nice!! I would take it a step further, and create a custom hook (assuming we’re using React, and doing some client side fetching) that utilises this logic in an effect, with states for request status (idle, loading etc.), data, and error. Great content!
@danielmazout
@danielmazout 5 ай бұрын
tanstack query vibes
@user-xu9tb7oe2z
@user-xu9tb7oe2z 5 ай бұрын
мужик, спасибо тебе огромное за этот бесценный труд!
@AnuMessi10
@AnuMessi10 6 ай бұрын
Great, what we do at our company is also have a custom wrapper class around axios that takes in the endpoint, request type (POST, GET, PATCH etc) and the data thus exposing a simple function that can be called in the view. However I am also of the opinion that such API calls and logic should be abstracted out and kept in a hook or a store so that the view remains clean and nice.
@SurinderSingh-jt3fs
@SurinderSingh-jt3fs 6 ай бұрын
Very helpful ❤️
@chienquoc4458
@chienquoc4458 6 ай бұрын
very useful content. thank you
@f-neto1
@f-neto1 6 ай бұрын
Awesome content! Quick question: how would you handle loading/pending status within this requestHandler implementation?? Thanks! Keep it up 👌
@cosdensolutions
@cosdensolutions 6 ай бұрын
That's outside of its scope! You would have to handle them manually by setting and updating states
@user-ej7ij2mi7l
@user-ej7ij2mi7l 6 ай бұрын
Amazing! What do you think about using axios over nextjs13v? If I use axios, I can't use nextjs fetch options (e.g. {cache: 'no-store' }).
@cosdensolutions
@cosdensolutions 6 ай бұрын
You can convert this to fetch without too much trouble and then use all of that!
@user-ej7ij2mi7l
@user-ej7ij2mi7l 6 ай бұрын
@@cosdensolutions thank you!!
@smithjohn2367
@smithjohn2367 6 ай бұрын
Really great thx. But if you have a token in the header and depending on the requests, headers that may have other properties or specific responseType, how would you do it? Aren't you going to pass the token to each request in the header?
@cosdensolutions
@cosdensolutions 6 ай бұрын
Hmm, interesting not sure. How would you do it in React Query?
@diegovinicius4186
@diegovinicius4186 26 күн бұрын
Question... since the return of the requestHandler is expecting a Promise, in the returns at the line #22 and #24, shouldnt be a Promise.resolve or reject?
@nodirbekeshonkulov3261
@nodirbekeshonkulov3261 6 ай бұрын
Really love your videos, thank you for your hard work, really appreciate it, could you please make some videos on next 14?
@i_am_pasindu
@i_am_pasindu 6 ай бұрын
Hi love your content, you are filling the gap which other developers couldn't demonstrate. 'How it works and why we are doing it' great content!! . But one more thing that is curious to me is you are not doing any NextJS content. Any plans for nextJS videos as well ? thank you. 👊
@cosdensolutions
@cosdensolutions 6 ай бұрын
I will make some nextjs content for sure in the future, but I haven't gotten there yet!
@lucasmachado6447
@lucasmachado6447 6 ай бұрын
If the resquestHandler were to be used in a client component, would the only differences be that getUsers is called inside a useEffect and that we would need to declare a piece of state to hold the response?
@cosdensolutions
@cosdensolutions 6 ай бұрын
Exactly!
@sahilhedau7377
@sahilhedau7377 5 ай бұрын
Sir, can you please make a video on transforming into TypeScript from JavaScript. I build react app in JS and I find many developers suggesting to use TS for better coding. But giving a big amount of time in learning new language is sometime tough. I am looking to learn TS in such a way that I can able to code my React Application using TS. I started some projects in TS, but stuck in various things like type errors while fetching data from API and all. Can you please guide me what should I do in order to hold TS good enough. Btw, I like your videos and really find it helpful and worth giving time. Thank You :)
@alirezaspeedy5951
@alirezaspeedy5951 5 ай бұрын
Have a question, what if we wants to use POST/PUT methods that required the data before configs as the second parameter?
@cosdensolutions
@cosdensolutions 5 ай бұрын
this will work out of the box with POST/PUT requests, but if you need to pass around some extra variables, you would have to add those to the types based on your needs!
@user-ke8of5mn1u
@user-ke8of5mn1u 6 ай бұрын
Amazing video can you cover some topics like CI/CD for react, Docker and all for full stack developer
@hareeshbhittam8069
@hareeshbhittam8069 5 ай бұрын
Hello, How can we handle post request with headers and body included?
@haoz5259
@haoz5259 6 ай бұрын
best ts course lol
@Huboh
@Huboh 5 ай бұрын
Just by the way, at 8:37, you can annotate “e” right there in the catch statement (eg: “} catch (e: E) {“ ). So you don’t have to cast e as E
@cosdensolutions
@cosdensolutions 5 ай бұрын
actually you can't. Typescript is strict about how you type errors
@viallymboma9874
@viallymboma9874 5 ай бұрын
Not a lover of typescript...but i have to get along and catch up with the tech pendulum
@matheuspassos8650
@matheuspassos8650 6 ай бұрын
Incredible! How can I do this without Axios? Just use Fetch?
@cosdensolutions
@cosdensolutions 6 ай бұрын
Yeah, but you'd need to change some types around like the axios response. Try it and see if you can figure it out ☺️
@Imam.Mehdi_
@Imam.Mehdi_ 6 ай бұрын
Can you make video on typescript+ asynchronous js,,,, as there are some promises types and some pre given interfaces which API gives , for example geoLocation API in js,,,as it give two callback,,,, and there interface are pre defined.....
@codingcitymini
@codingcitymini 5 ай бұрын
That's a great video, but why not use one axios instance to set up a base URL, eventually if we need to add tokens or any extra information in the header or anything else, I think it's way more comfortable.
@cosdensolutions
@cosdensolutions 5 ай бұрын
Absolutely you should do that! I just left that out because that part would be different across tools like fetch and I wanted to focus on what's common
@md.asifal-mahmud5952
@md.asifal-mahmud5952 6 ай бұрын
cool
@tristianchung8383
@tristianchung8383 4 ай бұрын
Will we see Remix content on your channel soon instead of nextjs
@cosdensolutions
@cosdensolutions 4 ай бұрын
I still primarily only make pure React videos, not even with nextjs. This just happend to have been running on a next project but it's the exception
@prashlovessamosa
@prashlovessamosa 6 ай бұрын
Thanks. What's coming next please tell
@ankitpradhan4183
@ankitpradhan4183 5 ай бұрын
better to just make one response type, i dont know but I dont like multiple union types, you still have to check for condition of success or failure anyway
@NathanLevia
@NathanLevia 6 ай бұрын
Nice content. That help me so much
@nayandey5010
@nayandey5010 6 ай бұрын
Hey I'm working on a project in react native but i really want to learn typescript , please make a video on it
@cosdensolutions
@cosdensolutions 6 ай бұрын
Well, there's great lessons here!
@Shawn-Mosher
@Shawn-Mosher 5 ай бұрын
Could you make a fetch example?
@cosdensolutions
@cosdensolutions 5 ай бұрын
just try it yourself, try replacing axios with fetch in the type definitions and see how you figure it out! Most of it will be the same just some types will be different and how your return the data
@igormizyk9437
@igormizyk9437 5 ай бұрын
Why wouldnt one just use axios interceptor instead?
@cosdensolutions
@cosdensolutions 5 ай бұрын
I'm not sure you can do this with interceptors in this way
@2gbeh
@2gbeh 6 ай бұрын
MORE CONFUSED
@robinparadise
@robinparadise 5 ай бұрын
Why axios, why it requires in avery project to add 30kB of a library that "fetch" can do it for 0Kb? Why 😫? whyyyyyyy 😫📣?
@cosdensolutions
@cosdensolutions 5 ай бұрын
You can use fetch if you want 😋
@HelenaDuGraus
@HelenaDuGraus 6 ай бұрын
"Simple"
@Grom1477
@Grom1477 6 ай бұрын
You should've mention that this is Next.js video. This whole thing is not applicable to pure React
@cosdensolutions
@cosdensolutions 6 ай бұрын
This has nothing to do with next js my dude, watch it again!
@kenzo3477
@kenzo3477 6 ай бұрын
Typescript just brings unnecessary complexity to ur code. Anyway, great content
@cosdensolutions
@cosdensolutions 6 ай бұрын
I would disagree actually, because with this you get type inference automatically and it really helps. You just have to deal with the complexity once when setting up so it's no big deal imo
@user-ku2sn1wz1c
@user-ku2sn1wz1c 6 ай бұрын
Only the people who have never used typescript could say it is not of help or unnecessary 😂
@user-ku2sn1wz1c
@user-ku2sn1wz1c 6 ай бұрын
Sorry, a little mistake, people who have never used it smartly
@GavHTFC
@GavHTFC 6 ай бұрын
I can understand your viewpoint at the very start of using it, or very early development of a system, but as time has progressed it really is a life-saver in catching bugs at build-time before they become run-time bugs.
@DioArsya
@DioArsya 5 ай бұрын
yup, and javascript itself brings unnecessary debugging process to ur code. Anyway, happy weekend
Learn React Hooks: useCallback - Simply Explained!
17:15
Cosden Solutions
Рет қаралды 68 М.
You Are Using useEffect Wrong
14:40
Cosden Solutions
Рет қаралды 29 М.
Не пей газировку у мамы в машине
00:28
Даша Боровик
Рет қаралды 10 МЛН
Don't eat centipede 🪱😂
00:19
Nadir Sailov
Рет қаралды 22 МЛН
React Junior Developer Interview (Questions & Challenge)
1:06:19
Cosden Solutions
Рет қаралды 102 М.
Времена жизни в Rust
19:29
Bitωise
Рет қаралды 2,3 М.
Generics: The most intimidating TypeScript feature
18:19
Matt Pocock
Рет қаралды 159 М.
Most React devs don’t understand generic components
5:43
Matt Pocock
Рет қаралды 45 М.
7 Senior React Developer Tools (Realistic Workday)
18:53
Cosden Solutions
Рет қаралды 11 М.
This is the Only Right Way to Write React clean-code - SOLID
18:23
Goodbye, useEffect - David Khourshid
29:59
BeJS
Рет қаралды 492 М.
How Did I Not Know This TypeScript Trick Earlier??!
9:11
Josh tried coding
Рет қаралды 200 М.
React Hook Form - Complete Tutorial (with Zod)
28:22
Cosden Solutions
Рет қаралды 73 М.