Why I Still Use React Query with Next 14

แชร์
ฝัง
  • เผยแพร่เมื่อ 26 พ.ย. 2024

ความคิดเห็น • 110

  • @Logicmontana
    @Logicmontana 10 หลายเดือนก่อน +24

    00:01 Is React Query still worth using with Next.js?
    01:18 Using React Hook Form and Next Safe Actions for form validation
    02:40 Fetching and handling post data with React Query
    04:08 Using React Query to handle data fetching and caching in Next.js
    05:42 Initialize and configure React Query in Next.js with a query provider file
    07:03 Using React Query for server-side rendering and data fetching
    08:34 Using React Query for managing state in client side components
    10:09 Demonstration of real-time data fetching and server-side validation

    • @developedbyed
      @developedbyed  10 หลายเดือนก่อน +4

      That's well impressive you did that in a minute, holy crap

    • @Logicmontana
      @Logicmontana 10 หลายเดือนก่อน

      @@developedbyed 😅 wasn't easy but you have been my only tutor from the start, don't need to watch the video twice to understand you .

    • @prashlovessamosa
      @prashlovessamosa 10 หลายเดือนก่อน

      You can also do this with AI Ed​@@developedbyed

  • @nathanblaing
    @nathanblaing 10 หลายเดือนก่อน +12

    I think there is still a strong argument to use React Query if you’re using queries from a variety of sources and/or complex filters that create dependencies. Lots of enterprise apps that I’ve worked on pull data from different sources (APIs, MSSQL, other relational databases etc) and consolidate on the client view. Including filters etc from global state in query keys (which operate as dependency arrays for a given query function) is an elegant pattern that just works.

  • @developedbyed
    @developedbyed  10 หลายเดือนก่อน +27

    Told ya I'll start posting more,
    hope you enjoy this one 👋

    • @Logicmontana
      @Logicmontana 10 หลายเดือนก่อน

      thats really useful

    • @emsy9471
      @emsy9471 10 หลายเดือนก่อน

      do you have discord server?

  • @wisdomelue
    @wisdomelue 10 หลายเดือนก่อน +27

    question is, is that not too many api calls, setting refetch interval to 6 seconds?

    • @mohammedsaad0761
      @mohammedsaad0761 5 หลายเดือนก่อน +1

      i have the same question ++

    • @paleo3142
      @paleo3142 4 หลายเดือนก่อน +2

      It's obviously too much ; you would rather use webhooks or other types of subscriptions to handle this. Probably not the best example, but hey, its just an example.

    • @mohammedsaad0761
      @mohammedsaad0761 4 หลายเดือนก่อน

      @@paleo3142 can you maybe share the blog link to the webhooks you talking about ?

  • @codingintelugu8820
    @codingintelugu8820 10 หลายเดือนก่อน +6

    Within Nextjs you can still pass const revalidate = 3600 at the top of your page or if you are using fetch youn use { next: { revalidate: 3600 } } to revalidate the cache on timelines. I'm still not able to figure out use case of React Query in Nextjs

    • @sudarshan7468
      @sudarshan7468 10 หลายเดือนก่อน

      same here

    • @aurorasofie
      @aurorasofie 9 หลายเดือนก่อน

      Wondering the same.

    • @rgsquintanilha
      @rgsquintanilha 9 หลายเดือนก่อน +5

      This will only work if the endpoint is public. If your fetch uses some sort of authorization (for example passing a cookie for authentication), then you are automatically opt-out.
      Another (arguably smaller) advantage is being able to cache locally. NextJS won't cache during development, so if your call takes long it can be annoying to work on.

    • @aurorasofie
      @aurorasofie 9 หลายเดือนก่อน

      @@rgsquintanilha What about using the fetch options, how does it compare? And, if you deploy on some other platform than Vercel, what changes? fetch('...', { next: { revalidate: 3600 } })

  • @aqibmalik6313
    @aqibmalik6313 10 หลายเดือนก่อน +2

    Deved I love you and your work..
    Here is a little motivation
    Keep going always no matter what. Somewhere in a small village of a small town in the state of Kashmir a boy is waiting for ur video

  • @ricoputrapradana4407
    @ricoputrapradana4407 10 หลายเดือนก่อน +5

    Hi Ed, just asking, does it mean you're using long polling approach there for getting the "real-time changes"?

    • @motta021
      @motta021 10 หลายเดือนก่อน

      this is a behavior of react query with the refetchInterval

  • @elenamikhaylova5086
    @elenamikhaylova5086 7 หลายเดือนก่อน +1

    I started watching your videos 5 years ago when i was first learning web development and I stumbled on your stuff again and I am so happy you are still making videos!! still as helpful as ever. thanks for the high energy and humor

  • @piotrra6281
    @piotrra6281 3 หลายเดือนก่อน +1

    Hey Ed great video! I have one question if I got it right. You mentioned (7:20) that you wanna keep Posts component Server Rendered for the SEO reasons. However later on, you transformed the Posts component into the client one. Then the assumption to have it SEO ready is not valid anymore, right? Thanks!

    • @hammadXP
      @hammadXP 3 หลายเดือนก่อน +1

      The hydrate boundary that we added to it's parent is doing all the magic, from what I've understood, this hydration technique by react-query helps in prefetching the data on the server just like server components

  • @mathiasriissorensen6994
    @mathiasriissorensen6994 9 หลายเดือนก่อน +1

    This is brilliant! I do wonder how to revalidate when something needs to get updated in my nav bar, which is a part of my layout and not a part of a specific route/page, where I'm mutating from.

  • @maskman4821
    @maskman4821 9 หลายเดือนก่อน

    This refetching approach (every 6 seconds) makes realtime database possible in SSR ❤

  • @theDanielJLewis
    @theDanielJLewis 25 วันที่ผ่านมา

    Do you refetch instead of using websockets because refetching is easier? Or is there a bigger reason to refetch instead of setting up a websocket?

  • @airixxxx
    @airixxxx 10 หลายเดือนก่อน +5

    but by using react query you had to make it a client component, right?

    • @enitan2002
      @enitan2002 5 หลายเดือนก่อน

      yes

  • @Logicmontana
    @Logicmontana 10 หลายเดือนก่อน +2

    i like the consistency keep it up dev ed thanks again

  • @usamecoban2582
    @usamecoban2582 10 หลายเดือนก่อน +3

    Hey Ed, thanks for the video. I have a question about this approach. You had an another video on your channel about react-query combo with nextjs14, I tried same methods on my project but when I fetch data just using server actions I can view all the html content by checking page source. But If I use hydration boundary with prefetch query, I dont see any content on the page source

    • @developedbyed
      @developedbyed  10 หลายเดือนก่อน +4

      Ohh that’s interesting, will definitely have a look into it. Their docs say using initial data and hydration boundary give you ssr, ill get back to you 👍

    • @finalfantasy7820
      @finalfantasy7820 9 หลายเดือนก่อน

      I'm guessing when you set your page component as client side component there's no way it's hydrated on the server side. You should probably keep your page component as server side, and call the action directly for initial server-side hydration. Then, create a client side wrapper around server component that utilises useQuery hook and pass the returned data to server component as prop as you'd normally do with getServerSideProps.

    • @rumonintokyo
      @rumonintokyo 7 หลายเดือนก่อน

      I have the same exact issue after setting up custom hooks of the tanstack query that fetches data from the client side... the data is shown but the page source does not update

  • @OhHeyalNo
    @OhHeyalNo 7 หลายเดือนก่อน

    How would I go about fetching posts that belong to a specific tenant ?
    I have a Supabase table for all my posts, but it's a multi tenant multi user app.
    One thing I'm struggling to get my head around with a drizzle query is getting all the posts that belong to the logged in users tenant ?
    Supabase doesn't have a field in the auth schema to put tenantID so I assign it in the public.users.tenant_id
    How do I get the logged in users tenant _id before I make the query ?
    I'm guessing it's going to come down to using RLS and just making a general query but I'd like to do it programmatically so I can address any bugs or issues from one control source.
    Love your videos, I'm a hobbyist and you've helped with heaps of niche situations I've come across.

  • @nguyenminhhieu8604
    @nguyenminhhieu8604 10 หลายเดือนก่อน +2

    Why not use revalidate in NextJs instead of ReactQuery? I watched your video through to the finish, but I'm not sure why you do it. Will you kindly respond to me?

    • @ooogabooga5111
      @ooogabooga5111 9 หลายเดือนก่อน

      you can just export revalidate variable and then wrap non fetch request in a react cache, the revalidate will get applied for it too.

  • @valokin
    @valokin 5 หลายเดือนก่อน

    I have a similar example and refetching is not working, the request never get made on the client?
    Could it be some server action caching issue?

  • @IvayloDechkoff
    @IvayloDechkoff 10 หลายเดือนก่อน +2

    Hi Ed, thanks for another great video! May I ask what theme you're using in VS Code?

  • @sansxverse
    @sansxverse 3 หลายเดือนก่อน +1

    I love your personality! Thank you for making helpful videos
    It's helping me a lot when I'm stuck in a company where I can't learn from the senior devs in my team, so I'm looking it up on youtube

  • @jaysolanki6572
    @jaysolanki6572 หลายเดือนก่อน

    What about Next.js data caching layer which persists the result of data fetches across incoming server requests and deployments??
    React Query could fetch the data on server but what about caching it across the server request?
    It's surely a solution in one scenario and providing caching solution for single user only.

  • @kimhwanhoon
    @kimhwanhoon 10 หลายเดือนก่อน +3

    I believe you don’t have to put “use server” on the top since the base is server. If you wanna make sure it only runs on server, you import “server-only”

  • @lukasunzeitig2064
    @lukasunzeitig2064 5 หลายเดือนก่อน

    @developedbyed Hello, i am having issue, that if queryFn is arrow function, i am getting error on client - Error: Maximum call stack size exceeded. But once i remove arrow, it works as your example.. But i need to pass properties to that function so it must be arrow function.. Any ideas ?

  • @roberth8737
    @roberth8737 10 หลายเดือนก่อน

    Thats a cool use of query. Are you mostly swapping actions in instead of trpc?

  • @alexambrinos
    @alexambrinos 10 หลายเดือนก่อน +1

    Hi, I just want to thank you for opening my eyes. I was thinking of either using the next-safe-action or the react hook form, not realising that i can just use them together to make a killer combo. I just have one question, if we are handling data cache using tanstack query, wouldn't we need to invalidate the cache when creating something new using something like invalidateQueries after being successful? Is it handled by the revalidatePath used in the action? Does it work because it removes the client side router cache and full route cache and so the query runs again on the server because the client makes a new request with the cleared cache?

    • @usamecoban2582
      @usamecoban2582 10 หลายเดือนก่อน

      I think revalidating path triggers the prefetch method in the server component. But didnt try just making a guess

    • @TheEdoRan
      @TheEdoRan 10 หลายเดือนก่อน +3

      I'm the creator of next-safe-action and I always used it with react-hook-form, I think it's an amazing combo. Very glad that now Ed is teaching this approach to the world!

    • @Tanner-cz4bd
      @Tanner-cz4bd 10 หลายเดือนก่อน

      @@TheEdoRan thanks for the amazing project

    • @Tanner-cz4bd
      @Tanner-cz4bd 10 หลายเดือนก่อน

      @@usamecoban2582 I was also thinking the same thing, can someone confirm this?!

  •  8 หลายเดือนก่อน

    How would you implement prefetch and tanstack table?

  • @mikelCold
    @mikelCold 6 หลายเดือนก่อน +1

    I'd say the proper solution is a websocket, but I'm not sure the best way. React Query is the easiest and good for state management.

  • @raves_r3177
    @raves_r3177 3 หลายเดือนก่อน

    when i do this in two routes there is an issue. When i leave the tab for a while then come back and then navigate back and forth to the two routes, it refetches the data again. (even if i set gctime and staletime to infinity and give refetchonwindowfocus:false on both queryclients)

  • @thepenetrator2006
    @thepenetrator2006 10 หลายเดือนก่อน +22

    I can’t programme anymore I swear work is killing me. I think I need another job.

    • @marcvanrenterghem5468
      @marcvanrenterghem5468 10 หลายเดือนก่อน +1

      What's the issue here ?

    • @developedbyed
      @developedbyed  10 หลายเดือนก่อน +11

      Hope you find something that brings back some of that joy. Wishing you best of luck

    • @IanJamieson
      @IanJamieson 10 หลายเดือนก่อน +2

      It's because everything is half baked. Ignore TH-cam 😅

    • @marcvanrenterghem5468
      @marcvanrenterghem5468 10 หลายเดือนก่อน +1

      Actually I was able to make an entire website in NextJS14 without react-query, only using next-safe-action library ;)

  • @Omarwaqar-pt7wf
    @Omarwaqar-pt7wf 10 หลายเดือนก่อน

    If are calling server action in a server component than there is no need for next-safe action right ? I don't know maybe it sound a bit dump question but I'm still learning

  • @frazic51
    @frazic51 4 หลายเดือนก่อน

    Hi @developedbyed, from 5:50 you created a useQuery provider & useGetPosts custom hook, why in Home at 8:03 do you need to recreate a queryClient and a query without what you've created before?

    • @raves_r3177
      @raves_r3177 3 หลายเดือนก่อน +1

      They are two different queryclients. in 5:50 the queryclient he created is a global 'provider' that enables you to use react query in all of your client components. In 8:03 he is making a queryClient for that server component. Because you need to make a new queryclient for each server component. Read the docs, under advanced server rendering.

  • @attilaborzak7557
    @attilaborzak7557 7 หลายเดือนก่อน

    Thanks to You, I was able to fix an error in my code: useQuery's queryFn needs to be on the server side, otherwise it will throw an error:
    Invariant: headers() expects to have requestAsyncStorage, none available.
    I think it's not documented well, or it's just Next.js things...

  • @thielicious2156
    @thielicious2156 10 หลายเดือนก่อน

    how come you get no type errors during all of that despite working in ts/tsx files. did u change or disable something?

  • @reactnareal
    @reactnareal 9 หลายเดือนก่อน

    Why not just use the fetch API and assign tags to revalidate the cache?

  • @edmondbounasr8727
    @edmondbounasr8727 9 หลายเดือนก่อน

    do we have to use revalidate with react query ? because u kept in ur action

  • @ae_holic
    @ae_holic 10 หลายเดือนก่อน +1

    May I know what your chilled background music is?

    • @developedbyed
      @developedbyed  10 หลายเดือนก่อน +1

      Description has all the tracks in this vid 👍

    • @ae_holic
      @ae_holic 10 หลายเดือนก่อน +1

      @@developedbyed Oh I already was binge reading the descrioption but I was blind :D thanks

  • @hypealex
    @hypealex 9 หลายเดือนก่อน

    Wrapping provider with usestate will resulted as an error: async/await is not yet supported in Client Components!

  • @trungtrung2248
    @trungtrung2248 5 หลายเดือนก่อน

    do react query support safari browser ??

  • @timeforrice
    @timeforrice 8 หลายเดือนก่อน

    Always enjoy your videos

  • @gaming_zen_99
    @gaming_zen_99 10 หลายเดือนก่อน +2

    Is SWR acceptable instead 🤔

  • @iscars
    @iscars 10 หลายเดือนก่อน

    You are using a new QueryClient on the server side without option staleTime (7:50). When I do that, it makes an API request every time without SSR caching. If I add staleTime Infinity, caching starts working, but updates from the client component revalidatePath or revalidateTag are not being executed. Help

    • @lucasmaia7553
      @lucasmaia7553 10 หลายเดือนก่อน

      I thought that was only happening with me, your code is being executed on the client side also right?

    • @iscars
      @iscars 10 หลายเดือนก่อน +1

      @@lucasmaia7553 I tried various ways to do this with React Query, but nothing worked. I came up with a method of loading data on the server side using native fetch. Then, I forcefully store the result in prefetchQuery. It looks like this: const result = fetch(); await queryClient.prefetchQuery({queryFn: () => result}). Now everything works as I want, but I have to maintain duplicate requests.

  • @simpingsyndrome
    @simpingsyndrome 10 หลายเดือนก่อน

    I only use RQ as my global state management and it's sooo great compared to silly rtk library,

  • @ooogabooga5111
    @ooogabooga5111 9 หลายเดือนก่อน

    you don't need to do any of that, you can just export next.js revalidate constent and set it to some posivite value for the page, and then wrap all the database query logic inside a react cache, now the cache will be revalidated as you set.

  • @_tanzil_
    @_tanzil_ 10 หลายเดือนก่อน

    Awesome vs code themeing. Please share your latest vs code setups. ❤

  • @Tommy-ev6gv
    @Tommy-ev6gv 10 หลายเดือนก่อน

    Hi Ed, One question regarding the fetching, is it because you use drizzle you get the types of posts?

    • @euanmorgann
      @euanmorgann 10 หลายเดือนก่อน

      Yes it is :)

    • @speedster784
      @speedster784 10 หลายเดือนก่อน

      Prisma also offers this

    • @Tommy-ev6gv
      @Tommy-ev6gv 10 หลายเดือนก่อน

      @@euanmorgann Thanks for the reply. I need to check it out !

    • @developedbyed
      @developedbyed  10 หลายเดือนก่อน +1

      Yeah that’s correct! They both offer it, I went with drizzle cause it’s simple and fast

  • @learner8084
    @learner8084 10 หลายเดือนก่อน

    Thanks very much for the video. I wonder if server actions works with react native programs ? Or I still need to use API ? Also what network protocols does the server action use ? Would it be possible to route through firewalls ? Thanks again.

    • @metcaelfe
      @metcaelfe 3 หลายเดือนก่อน

      HTTP POST

    • @learner8084
      @learner8084 3 หลายเดือนก่อน

      @@metcaelfe Thanks. I've later learnt that server actions are an abstraction of APIs.

  • @ukaszzbrozek6470
    @ukaszzbrozek6470 10 หลายเดือนก่อน

    If you are using the next js BE isn't better to use tRCP instead React Query ?

    • @Tanner-cz4bd
      @Tanner-cz4bd 3 หลายเดือนก่อน

      tRPC is using react query

    • @ukaszzbrozek6470
      @ukaszzbrozek6470 3 หลายเดือนก่อน

      @@Tanner-cz4bd That don't change tRPC is a more comprehensive tool.

  • @juanmacias5922
    @juanmacias5922 10 หลายเดือนก่อน

    From my Firebase experience using web sockets/subscriptions to keep the list updated this approach is very different lol

  • @berkaycirak
    @berkaycirak 8 หลายเดือนก่อน

    I guess, it has a really bad design when user wrap it client component with hydration boundary and passing dehydrate function. It should be simple and clean, in my opinion

  • @lucasmaia7553
    @lucasmaia7553 10 หลายเดือนก่อน

    Amazing content as always. One thing that I got sad about is, I downloaded your project and I realized that the data comes to the client side with requests so the functions apparently are not dispatch on the server side but indeed on the client side, so in theory you won’t need any of this right? You can double check the console of your browser will have the “creating post…” message, I thought this would be executing on the server side 😢

  • @svetoslavtrifonov6431
    @svetoslavtrifonov6431 10 หลายเดือนก่อน

    Hi, great video. Sadly I have this error and I can't get rid of it...
    Internal error: TypeError: Cannot read properties of null (reading 'useContext')
    This is when I use the prefetch and HydrationBoundary
    I think it is only in development but still, if anyone has any ideas. Thanks

    • @ervinbehxheti344
      @ervinbehxheti344 10 หลายเดือนก่อน

      Any error that says cannot read properties of null means that the variable or reference you are using has no value

    • @ervinbehxheti344
      @ervinbehxheti344 10 หลายเดือนก่อน

      or probably doesnt exist

  • @siebe681
    @siebe681 7 หลายเดือนก่อน

    great video!

  • @Alex.Shalda
    @Alex.Shalda 7 หลายเดือนก่อน

    Love your videos

  • @jazzymichael
    @jazzymichael 10 หลายเดือนก่อน +3

    It looks like you just implemented a very basic polling solution to refetch all data every 6 seconds...

    • @trebturner
      @trebturner 10 หลายเดือนก่อน +1

      How would you implement it without react query?

    • @inzendis
      @inzendis 10 หลายเดือนก่อน

      @@trebturnerif using Next, I would create a refresh component with the time I want. It will use UseRouter from next/nav and do router.Refresh(). Obv you would need to disable caching with giving headers to the Get req or revalidate = 0.

    • @juancamiloibanezchaparro
      @juancamiloibanezchaparro 9 หลายเดือนก่อน +1

      @@trebturner set interval with a basic setting cache ? what you do you need more

  • @gustavokuhluna
    @gustavokuhluna 10 หลายเดือนก่อน

    What about SWR?
    It's so simple, looks fine to my, but I don't know nothing about the comparison between other strategies

    • @Tanner-cz4bd
      @Tanner-cz4bd 10 หลายเดือนก่อน +3

      Lack of updates and poor docs

  • @Tanner-cz4bd
    @Tanner-cz4bd 10 หลายเดือนก่อน

    this is great

  • @regilearn2138
    @regilearn2138 10 หลายเดือนก่อน

    pls do MERN stack next js project with TYPESCRIPT ,react query and REDUX

    • @t-shirtguy
      @t-shirtguy 8 หลายเดือนก่อน

      Redux??

    • @LukeCreates
      @LukeCreates 3 หลายเดือนก่อน

      No one uses mongo anymore, hate to break it to you. And Redux is awful, Zustand (or no state manager at all) is much better

  • @jellyfish1772
    @jellyfish1772 8 หลายเดือนก่อน

    50 shades of cache.hihi

  • @sarcasticdna
    @sarcasticdna 10 หลายเดือนก่อน

    No next js, I dropped using it

  • @Santiego
    @Santiego 9 หลายเดือนก่อน

    Web devs are masochists lol, there is so much boilerplate