Тёмный

Posting Data to Server from React - Query Updates from Mutations 

Leigh Halliday
Подписаться 33 тыс.
Просмотров 16 тыс.
50% 1

Опубликовано:

 

3 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 97   
@rorysandstrom6555
@rorysandstrom6555 4 года назад
"I've lost three friends this morning." Times are tough up there in Canada. Great content as always, Leigh.
@leighhalliday
@leighhalliday 4 года назад
:D Thanks Rory! Appreciate the support!
@sourenasahraian2055
@sourenasahraian2055 2 года назад
hustling to put the video out and get things sorted out right before the stand up , I know that race against time . Appreciate the Impeccable delivery.
@brielov
@brielov 4 года назад
I'm Argentinian and I love see you "tomás mate"
@leighhalliday
@leighhalliday 4 года назад
Clari que si! Tomé otro hoy :)
@diegazo5556
@diegazo5556 3 года назад
YERBA MATE! Cheers dude! My regards from Argetina, land of Yerba mate!
@leighhalliday
@leighhalliday 3 года назад
I will drink a Yerba mate today in your honour
@nijandhanjaganathan5281
@nijandhanjaganathan5281 3 года назад
Thank you so much for the amazing tutorial..
@angristan
@angristan 3 года назад
Cristal clear. Thanks!
@uriel4829
@uriel4829 4 года назад
Thanks for the awesome video! I was stupidly stuck on the name for what I now know is called "optimistic cache" updating. This vid helped alot.
@leighhalliday
@leighhalliday 4 года назад
You're welcome! Glad you enjoyed it :)
@jbradshaw7
@jbradshaw7 3 года назад
Explained it all very well thanks.
@leighhalliday
@leighhalliday 3 года назад
Thank you Jeremy!! Glad you enjoyed it.
@fortoday04
@fortoday04 2 года назад
You're very cool Leigh. Very cool.
@leighhalliday
@leighhalliday 2 года назад
Haha thanks Destroyer!
@frederikclement7795
@frederikclement7795 Год назад
Great video, thank you
@genc_emekli
@genc_emekli 3 года назад
Many thanks for the excellent video and, as always, especially for putting the sample code in Github!
@leighhalliday
@leighhalliday 3 года назад
You're welcome Mustafa! Glad you enjoyed it!
@russelldias1998
@russelldias1998 4 года назад
Great stuff! Extremely detailed!
@leighhalliday
@leighhalliday 4 года назад
Thanks Russell, glad you enjoyed it!
@abessesmahi4888
@abessesmahi4888 4 года назад
Welcome back Leigh thank you so much
@leighhalliday
@leighhalliday 4 года назад
Thanks Abesse!! :D Hope you enjoy it!
@abessesmahi4888
@abessesmahi4888 4 года назад
@@leighhalliday of course
@ridl27
@ridl27 4 года назад
you always choose very interesting topics for lessons. ty :)
@leighhalliday
@leighhalliday 4 года назад
Hehe, thanks Alex! I just choose whatever is interesting to me :)
@user-ww6ns4hn9r
@user-ww6ns4hn9r 3 года назад
7:30 usetMutations does give you and object if you want to use that: const {mutate, isLoading, isError} = useMutation(...)
@michelkant
@michelkant 4 года назад
Great video! Awesome idea that i will for sure integrate in to my code.
@leighhalliday
@leighhalliday 4 года назад
Thank you Michel! Glad you enjoyed it :)
@marcgagnon1277
@marcgagnon1277 3 года назад
Thanks so much, and as a Canadian currently in Switzerland, I report no bear sightings so far...
@leighhalliday
@leighhalliday 3 года назад
Hey Marc!! Stay vigilant!
@yaroslavoz
@yaroslavoz 4 года назад
I'm your new fan! You make a wonderful useful videos
@leighhalliday
@leighhalliday 4 года назад
Thank you :D I appreciate the support!
@chamir4614
@chamir4614 4 года назад
Great tutorial dude, thank you very much :)
@leighhalliday
@leighhalliday 4 года назад
Thanks Chamir! Glad you enjoyed it :)
@galiprandi
@galiprandi 3 года назад
¿Cómo está ese mate? Saludos desde Argentina!
@matheusvictor9629
@matheusvictor9629 4 года назад
that was great, thanks!
@leighhalliday
@leighhalliday 4 года назад
Thanks for saying hi, Matheus! Glad you liked it!
@chany0033
@chany0033 3 года назад
Programar y tomar mate es lo que hago de lunes a viernes jaja, saludos desde Argentina.
@leighhalliday
@leighhalliday 3 года назад
Jajaja... somos iguales pero en otros lados del mundo :D Voy a tomar un mate hoy mismo!
@TomHermans
@TomHermans 4 года назад
There's bears everywhere, there's bears outside. I lost 3 neighbours this morning. So ...... it's very important to save your data ! hahahaha. Brilliant
@leighhalliday
@leighhalliday 4 года назад
Haha... thanks Tom :) Glad you enjoy my lame jokes!
@jeztudios329
@jeztudios329 4 года назад
I love you, thank you soooooooo much!!!!
@leighhalliday
@leighhalliday 4 года назад
:) Glad you enjoyed it!
@ladankavandi4923
@ladankavandi4923 3 года назад
for more information i use from nextjs
@DonatoAguirre
@DonatoAguirre 4 года назад
Hi Leigh, thanks for another great video!! I knew you speak Spanish, but now seeing you drinking mate, I wonder if you have relatives in Argentina or Uruguay? Cheers from Uruguay btw ;)
@leighhalliday
@leighhalliday 4 года назад
Hehe... no, no relatives from there. My wife is Colombian though which is where the Spanish comes from... the yerba mate is just because I enjoy it :D
@Norfeldt
@Norfeldt 3 года назад
your videos are always super useful and informative ❤️ I'm curious to know how react query refetching works together with react memo?
@leighhalliday
@leighhalliday 3 года назад
React memo typically means don't re-render the component unless its props change or its internal state changes, so I think as long as one of those things occurs, you're good to go.
@Norfeldt
@Norfeldt 3 года назад
@@leighhalliday sorry if my question was vague described. Guess it's because I'm thinking of a more complex scenario that was a couple of questions to it. Haven't used react query but it seems to solve a couple of common issues I run into. Will try to describe the scenario I'm thinking of better. Say you have an API call that returns an large object that needs to be distributed to a lot of different components. Would you then put that state into a React.Context or dig it out queryCache? (1) If you need it to have it act like a pseudo webhook and set it to expire the cache frequently. How would you avoid rerender if the returned state is unchanged?(2) or only rerender the changed state.. - React.memo uses === so I don't expect it to work (but could be wrong) I'm very curious about the use of queryCache vs React.Context - sometimes we need to enrich the state that comes from the backend by making a derived state (doing some logic on the server state - like adding some state values together). Would you then go in and mutate your cache or save that in React.Context? (3)
@deepakchandani5237
@deepakchandani5237 4 года назад
Hey Leigh, Thanks I enjoyed the video. but there is one thing we can correct. the return value of "onMutate" is passed as 3rd argument to "onError" function. www.npmjs.com/package/react-query#usemutation so we need to set `queryCache.setQueryData('sightings', oldData)` inside "onError" fn. or we can return rollback function from onMutate, eg: return () => queryCache.setQueryData('sightings', current)
@leighhalliday
@leighhalliday 4 года назад
Hey Deepak, you're right! Someone had already mentioned it and I added a note in the description and updated the source code... unfortunately can't correct a video once it's published. Thanks :)
@mortezatourani7772
@mortezatourani7772 4 года назад
Thanks Leigh. It was really cool. I have a question which is somewhat related and it's about optimistic response in Apollo. Unlike react query which gives you the data you want to send to server, all I found on Apollo suggests I have to create the gibberish object - which will be added to query - myself. Is there any better approach or that's all I've got? BTW, stay home and there would be no worries about Covid-19 and also bears :p
@leighhalliday
@leighhalliday 4 года назад
Hey Morteza! Thank you! I'd love to answer but I haven't really done too much work with optimistic responses in Apollo... seems like something I should try to dig into and make a video about :) Staying as safe as I can haha!!
@Amoz21
@Amoz21 9 месяцев назад
What if i need new inserted id instead of temporary id for dynamic route of detail page just in case it doesn't get /undefined
@Allyourneedsmet
@Allyourneedsmet 3 года назад
Do we do this for every API call and if not, how can we extract this to a reusable hook.
@ladankavandi4923
@ladankavandi4923 3 года назад
that was great, thanks but i have question i tried many times but querycach value is not read able i used quryclient instead const queryClient = new QueryClient(); and then queryClient.setQueryData but it did not work!!! even queryClient.setQueryData also did not work i dont know why can u help me
@flagoon
@flagoon 4 года назад
Seeing that you are responding to questions, is queryCache some version of global state object? Can I call it in different component and expect 'sightings'? If I had different api calls, will it create some kind of store?
@leighhalliday
@leighhalliday 4 года назад
Hey flagoon! Good question... honestly I'm not sure! I think it'd act as a global store, and would cause re-renders as long as you're using `useQuery` hook... but I think you'd have to give it a try :D
@ryandick6389
@ryandick6389 3 года назад
Your content is better than the content made by the creator of react-query. You should actually ask them to be paid for your content
@leighhalliday
@leighhalliday 3 года назад
Thank you :) react-query is just one guy though (twitter.com/tannerlinsley), all credit goes to him!!
@getme9103
@getme9103 3 года назад
Got a doubt In the onMutate() callback query data was fetched on line 33 before the new sightings was added in line 35. But you are returning the current variable which was fetched before setting the new cached data. Could you please explain this concept. This might be a silly question but i would appreciate if you could give me an explanation
@leighhalliday
@leighhalliday 3 года назад
The value you return from onMutate (github.com/leighhalliday/next-prisma-demo/blob/master/pages/query.js#L39) is actually a function to be called if you want to "revert" the change. The actual update happens on line 34.
@migom6
@migom6 4 года назад
swr vs react query ? any thoughts/preference ?
@leighhalliday
@leighhalliday 4 года назад
Both are pretty great! I should do the same vid but with swr for comparison :)
@CoryTheSimmons
@CoryTheSimmons 4 года назад
@@leighhalliday Just comparing the react-query API in your video vs SWR's, SWR seems way cleaner... I want those react-query-devtools for SWR though. 😭
@pooyatolideh9527
@pooyatolideh9527 4 года назад
Can we get an app tutorial for Raccoon Sighting in GTA? :))
@leighhalliday
@leighhalliday 4 года назад
Working on a memorial website for this guy www.thestar.com/news/gta/2015/07/09/torontonians-create-memorial-for-dead-raccoon.html
@BorisEdigarian
@BorisEdigarian 4 года назад
Do you think react-query + Context can fully replace Redux in big apps ? The concept is great , but Redux data flow is very simple to debug and DevTools are a great tool when you have problems.
@leighhalliday
@leighhalliday 4 года назад
Hey Boris! Honestly not sure because I've never worked with a large Redux app :D I'd be tempted to say yes... typically the larger apps I've worked on are GraphQL, which I think has a sort of similar caching idea to react-query, but with implicit cache rather than explicit
@indersingh8113
@indersingh8113 4 года назад
Hello Leigh. I am working on next js project with custom server. Can i keep next api routes in pages folder and custom server separate or do i need to add api routes in custom server? I am only using custom server for socket.io and all the api routes are in pages/api folder. Your videos are great!!
@leighhalliday
@leighhalliday 4 года назад
Hey Inder! I'd love to help but I'm honestly not sure... I haven't worked much with custom servers. Maybe someone else here knows?
@4541047
@4541047 4 года назад
Hi Leigh! If you do use functional component, why the server call is not in a useEffect() {serverCall() },[] hook? isn't how it should be?
@leighhalliday
@leighhalliday 4 года назад
Hello 4541047! If you were doing a fetch/axios call on your own, you're right, but since we're using react-query, it handles all of that stuff for us.
@juanruben
@juanruben 4 года назад
Hi Leigh! Thanks for your very nice tutorial. I have an issue in the onError function.. I implemented just as you did, but when I force an error to occurs it keeps saying that rollback is not a function
@leighhalliday
@leighhalliday 4 года назад
Hey! It was an error on my part when I made the video... what you return should be a function, like here: github.com/leighhalliday/next-prisma-demo/blob/master/pages/query.js#L39
@juanruben
@juanruben 4 года назад
@@leighhalliday Thanks! Your channel is very nice
@klapec_
@klapec_ 4 года назад
Why is it calling GET /api/sightings any time you POST a mutation?
@leighhalliday
@leighhalliday 4 года назад
I did three examples, and I think two of them had subsequent GET requests: 1) I was calling "refetchQueries" which kicks off the additional GET request. 2) I wasn't calling refetchQueries... shouldn't be an additional GET request here. 3) This one in onSettled calls refetchQueries which kicks off an additional GET request. Basically the idea is to re-fetch, so that your UI has the latest data from server, rather than only relying on manually updating the cache.
@klapec_
@klapec_ 4 года назад
Got it, thanks!
@mustafwm
@mustafwm 4 года назад
Hi Leigh, Thanks for the video. Just letting you know, I think the code snippet at the end is incorrect (the return current statement should be return () => queryCache.setQueryData('todos', previousTodos)) src: github.com/tannerlinsley/react-query#updating-a-list-of-todos-when-adding-a-new-todo FYI: as of version 2 refetchQueries is invalidateQueries
@mustafwm
@mustafwm 4 года назад
either that or the rollback function can be onError: (error, newData, snapshot) => queryCache.setQueryData('sightings', snapshot),
@leighhalliday
@leighhalliday 4 года назад
ahh yea, you're totally correct.. my bad, didn't test the error scenario :D I'll update source code and leave a note in description. cheers!
@nocoolming
@nocoolming 4 года назад
Can you write a blog for this example?
@leighhalliday
@leighhalliday 4 года назад
The docs are pretty great! I would check them out :D github.com/tannerlinsley/react-query#query-updates-from-mutations
@nocoolming
@nocoolming 4 года назад
@@leighhalliday thank you.
@MrProkiter
@MrProkiter 4 года назад
I can't delete using react-query plz help
@leighhalliday
@leighhalliday 4 года назад
You can do it! Don't give up!
@MrProkiter
@MrProkiter 4 года назад
True, i finally did that 😊
@oiojin831
@oiojin831 4 года назад
what would u use, swr vs react-query ?
@leighhalliday
@leighhalliday 4 года назад
I like 'em both, don't make me choose! Try them out and choose whatever you feel more comfortable with! Or go by the one that has a feature you need... or which has more repos using it (swr).
@susbedoo
@susbedoo 4 года назад
Can I use react-query instead of redux ?
@leighhalliday
@leighhalliday 4 года назад
You can do a lot of your server side state or external data fetching with react-query, but it won't replace your app state. Take a look at my video "How I Manage State in React"
@susbedoo
@susbedoo 4 года назад
@@leighhalliday I have already watched that video. You used a mix of local state, global state and server side state to manage the app state rather than dumping all of this in a global store. I hope we could talk more about my issue. It would be a great help.
@keepforever726
@keepforever726 4 года назад
I would like to suggest you grab yourself a USB mic. You can 1000x improve the quality of the audio by investing $50 in a mic. I'm an audiophile/musician, so, I would drop a little extra cheese above $50 (ideally something with gain control). Would go a long way for the "watchability" of your channel, which, content-wise, is top-notch.
@leighhalliday
@leighhalliday 4 года назад
Hey Brian! I actually have this one here: www.rode.com/microphones/podcaster but I'm in a super tiny office without any noise cancelling, so I think the echoing is what's making it sound so bad. I'm recording with ScreenFlow, which doesn't give me too many options to correct (at least on my version), so I think I have to invest in some foam acoustic sound panels... any tips???
@keepforever726
@keepforever726 4 года назад
@@leighhalliday Ah, gotcha. Yeah, that's some echo... looks like you have a smooth/wooden floor in there too. Acoustic panels are a good start. Maybe put down a rug in there. Or try and bring the mic closer to your face with one of those mechanical arms? That way, it'll pick up the sound directly from your mouth more prominently than the reflections? Just one of those things you gotta "dial in". Good luck!
@comment8767
@comment8767 2 года назад
latitude 97.3 ....
Далее
How I Manage State in React
17:09
Просмотров 33 тыс.
The Most Important Design Pattern in React
35:04
Просмотров 61 тыс.
КОТЯТА НАУЧИЛИСЬ ГОВОРИТЬ#cat
00:13
Тренд Котик по очереди
00:10
Просмотров 220 тыс.
React Query tips from the maintainer @tkDodo
16:19
Просмотров 24 тыс.
I tried 8 different Postgres ORMs
9:46
Просмотров 414 тыс.
Redux Toolkit Query vs React Query
22:12
Просмотров 108 тыс.
Drag n' Drop Files in React - Parsing CSVs
16:12
Просмотров 20 тыс.
The Value of Source Code
17:46
Просмотров 59 тыс.
КОТЯТА НАУЧИЛИСЬ ГОВОРИТЬ#cat
00:13