React's new cache function

Поділитися
Вставка
  • Опубліковано 5 сер 2024
  • In this video we're going to look at how React's cache function helps avoid unnecessary data requests.
    - 0:00 - Intro
    - 0:34 - Loading the current user
    - 2:31 - Using React’s cache function
    - 4:06 - Why not use props?
    - 7:51 - Good things to know about cache
    Preloading data with cache: • Preloading data (React...
    Check out my course "Data fetching with React Server Components" here: buildui.com/courses/react-ser...

КОМЕНТАРІ • 59

  • @bideshbanerjee5506
    @bideshbanerjee5506 5 місяців тому +4

    So basically when RSC starts to render, wherever it finds the cached function first it stored the result and uses it down the tree. It basically happens on every request.

  • @huzaifahattah6481
    @huzaifahattah6481 5 місяців тому

    Been watching your videos today and I have fallen in love with React again. Thank you.

    • @RyanToronto
      @RyanToronto  5 місяців тому

      Wow, that's great to hear! Love React!

  • @Delba
    @Delba 6 місяців тому +2

    Great step by step walkthrough. Thank you!

  • @cookiemonstr9071
    @cookiemonstr9071 5 місяців тому

    Amazing definitely gonna use in my next project. Thanks and keep uploading videos like this ❤

  • @hemantwasthere
    @hemantwasthere 6 місяців тому +3

    This is crazyy 🔥

  • @LutherDePapier
    @LutherDePapier 5 місяців тому

    Nice!! I didn't get it at first when I read the article on BuildUI without watching the video yet, but now it makes complete sense. And with Next.js's default caching, it makes for a pretty effective combo. 😁

  • @asimalqasmi7316
    @asimalqasmi7316 6 місяців тому +2

    Cool
    Clear explanation

  • @darkcss1054
    @darkcss1054 5 місяців тому

    I am brand new to React but your explanation is very clear even to me.

  • @haritssyah7434
    @haritssyah7434 6 місяців тому

    Thank you Ryan!

  • @tszhong0411
    @tszhong0411 5 місяців тому

    Love your content!❤ keep it up

  • @wdvhi
    @wdvhi 5 місяців тому

    subscribed and looking forward to more such videos...

  • @janisbasisbasovsdev7147
    @janisbasisbasovsdev7147 6 місяців тому +4

    I think Nextjs have integrated this cache by default. But they have also integrated extra 30 seconds cache on client side as well and without possibility to opt out of it. The 30 seconds client side cache makes it impossible to create Admin Dashboard with "live" data with only server components :(

    • @RyanToronto
      @RyanToronto  6 місяців тому +2

      Cool, thanks for sharing!
      React's cache is built right into React, but Next.js also provides some additional caching features in addition to `cache()`. Next has a lot of caching: build cache, fetch cache, client side router cache, and then on top of it there's the React cache... it can be so confusing to know which-is-which!
      I ran into the admin dashboard with live data problem a few months ago... Here's how I solved it: ua-cam.com/video/-t3-rG5G5tA/v-deo.html It's not pretty, but it seems to be working so far :)
      Thanks for the comment, I love hearing about how people are using RSCs.

    • @ontanicolae1794
      @ontanicolae1794 5 місяців тому +2

      cant you export dynamic = 'force-dynamic'?
      but why would you even want it to be a server component if data is always dynamic

    • @janisbasisbasovsdev7147
      @janisbasisbasovsdev7147 5 місяців тому

      @@ontanicolae1794 I dont want to expose fetched data to client. I want to return a ready HTML with latest data. The user doesnt need to know how and where do i get my data from. :)

    • @CistiC0987
      @CistiC0987 5 місяців тому +2

      ​@@ontanicolae1794
      Exactly my thinking. The caching especially that server cache is supposed to help your static pages. How is a real time dashboard a static component. I may be wrong here, so sorry if I sound patronizing, it was not my intention. I just think that it's not that everything should be built only with RSC components now. We should use them where they make sense.

  • @Luisllaboj19
    @Luisllaboj19 4 місяці тому

    I hope you can help me with this:
    when the unstable_cache from next becomes stable, both react and next cache wrappers will be imported as "cache"?
    if so, whats the best practice there, in case you need to use both cache functions? Or is it better to use next's version? :c thank you

    • @RyanToronto
      @RyanToronto  4 місяці тому +1

      Good question!
      I'm not sure about the naming. If I had to bet, one of them might get renamed... but we'll have to see. They're both unstable/canary APIs right now.
      React's cache and Next's unstable_cache do different thins. React cache is per request, it's very short lived. Next's cache is shared across all requests, and can last until it is invalidated. So there are different situations when you'd want to use one over the other.

  • @godkid.
    @godkid. 5 місяців тому

    is it accessible in nextjs or does declaring a server function behave the same without react-cache

    • @RyanToronto
      @RyanToronto  5 місяців тому

      Hmm I'm not sure what you mean? Do you have an example in mind?

    • @godkid.
      @godkid. 5 місяців тому

      @@RyanToronto Ooh nevermind i have gotten my answer after doing my research

  • @stevebendersky2056
    @stevebendersky2056 5 місяців тому +1

    in nextJS, the fetch function is automatically cached, right?

    • @RyanToronto
      @RyanToronto  5 місяців тому +1

      I believe so. The Next fetch cache is a long lived cache that is shared between multiple requests to your application. React's cache() is a per-request cache that is only active for the current lifecycle of the request.

  • @gustavoherrera8616
    @gustavoherrera8616 5 місяців тому

    how did you do to make async functional components? I though that was impossible!

    • @RyanToronto
      @RyanToronto  5 місяців тому +1

      With React Server Components you can use async/await. These components only run on the server and have different features compared to regular React components... one of those features is they can use async/await.

  • @snake1625b
    @snake1625b 5 місяців тому +1

    Do caching libraries like react query do a similar thing?

    • @RyanToronto
      @RyanToronto  5 місяців тому +1

      Great question! Libraries like react-query do have their own caching mechanisms, but those libraries have a slightly different use case. Client side data fetching libraries (like react query) are meant to run in the browser and maintain a long lived client side cache. React's cache function is meant to be short lived and only run while rendering RSCs.

  • @AnasKhan-fi7xk
    @AnasKhan-fi7xk 5 місяців тому

    I have one query,
    Then what's the benefit of using useCallback then in client

    • @RyanToronto
      @RyanToronto  5 місяців тому

      Sorry, I'm not quite sure I understand your question. If you only have one query, and it's only called once, then there's probably not much benefit to using cache.

  • @lambmaster
    @lambmaster 28 днів тому

    Awesome! But, why is this cache function in the React package, and not some Next package, especially if "vanilla" react doesn't even use it?

    • @RyanToronto
      @RyanToronto  28 днів тому

      Great question! Cache is in the main react package because it's useful for any React Server Component to use.

  • @user-zt1hx3ci2v
    @user-zt1hx3ci2v 5 місяців тому

    May be I am out of date, but can we use async with components ?

    • @RyanToronto
      @RyanToronto  5 місяців тому

      Yup! With React server components (currently in the canary version of React) you can run async components on the server. It's pretty awesome.

  • @garikmelqonyan6011
    @garikmelqonyan6011 3 місяці тому

    👍

  • @glenwinbernabe8937
    @glenwinbernabe8937 Місяць тому

    hello Ryan, how can I use the cache function, I don't know how to get it from the React Canary. Great video by the way that's why I want to use it in my project.

    • @RyanToronto
      @RyanToronto  Місяць тому

      The cache function is only available for React projects that use Server Components. If you're using RSC you can `import { cache } from "react";` and it will work. If you're not using RSC then cache is a no-op and doesn't do anything. Hopefully that helps!

    • @glenwinbernabe8937
      @glenwinbernabe8937 Місяць тому

      @@RyanToronto hello Ryan, how do I know if I am using RSC? What is an example of a Server Components?

    • @glenwinbernabe8937
      @glenwinbernabe8937 Місяць тому

      @@RyanToronto hello, it has something to do with the React Canary?

  • @zakidzz
    @zakidzz 4 місяці тому

    its experimental cache ? is it safe to use

    • @RyanToronto
      @RyanToronto  4 місяці тому

      It's in canary, but it's safe to use with React Server Components today.

    • @zakidzz
      @zakidzz 4 місяці тому

      @@RyanToronto any other alternative my goal is to read url params on deeply nested seever side component like use prams but down

  • @user-up9vx6cp6d
    @user-up9vx6cp6d 5 місяців тому

    what alternative for client server? t-t

    • @RyanToronto
      @RyanToronto  5 місяців тому

      Hmm, not quite sure what you mean. Do you have an example in mind?

  • @rushabhsheth08
    @rushabhsheth08 6 місяців тому +1

    @RyanToronto - this video is really timely because I’ve got a bunch of Prisma queries that aren’t cached.
    That being said, how does caching work with parameters that can change?
    Ex - a list of posts with each post component/card calling for views from the db

    • @RyanToronto
      @RyanToronto  6 місяців тому +2

      Great question! There will be different cache entries for each parameter, so if you have a cached getPost function and call getPost(1) and getPost(2), the function will run twice (once for parameter 1, and another time for parameter 2).

    • @rushabhsheth08
      @rushabhsheth08 5 місяців тому

      @ryantoronto but it wouldn’t invalidate the cache for Id 1, correct? so other references to the cached function could still benefit from cached value of id 1?

    • @RyanToronto
      @RyanToronto  5 місяців тому +2

      @@rushabhsheth08 Correct! No matter how many times you call getPost(1) it will always return the result from the first time. getPost(1) has it's own cache :)

  • @purpinkn
    @purpinkn 6 місяців тому

    in what world is 2s response on localhost acceptable?
    FOR A HELLO WORLD

    • @willianf
      @willianf 5 місяців тому

      Seriously, couldn't you abstract that the "sleep" function Ryan added to his function was a simple snippet demonstrating some latency without needing to connect to a real database or call an external API? It is called "simplicity" ;)

  • @miko007
    @miko007 5 місяців тому

    but pretty much everyone is going to use a store anyways...