React Cache Function Explained
Mar 15, 2024
0
React Cache Function which is right now in canary version, but going to introduce in React 19 mostly likely. It's help to cache the result of a data fetch or computation. But right now we can only use cache function in React Server Components. Since next.js already support React Server Components, so we can use it in next.js.
Unlike React Query, React Cache Function is not a library, it's a built-in feature of React. It's a low-level API that allows you to cache the result of a data fetch or computation. It's not a replacement for React Query as It's only work with React Server Components.
It's also very different from other React useMemo
or useCallback
hooks. It's not a hook, it's a function that you can call from anywhere in your component. It's also not a replacement for useMemo
or useCallback
hooks.
Key Features of React Cache Function
cache
only can use with React Server Components.- React will invalidate the cache for all memoized functions for each server request.
- It can cache all type of data, like if here
cache(fn)
infn
return error, then it will cache the error as well.
Usage of Cache Function#
In the above example, we are using cache
function to cache the result of getPost
function. So, when we call getSinglePost
function, it will return the cached result if it's already cached.
How to efficiently use Cache Function#
There are few things that you should keep in mind while using cache function. Always consider to keep cache function
in a separate file, so that you can import it anywhere and use same cache all over the places.
Keeping cache
in seperate file only needed if you want to use the same cache all over the places. It recommended to keep it in a separate file, so that you get good performance improvement over cache hit.
In the above example, we are using cache
function in a separate file, so that we can import it anywhere and use so the all places will use the same cache.
Nginx as Reverse Proxy for Kubernetes Services
Nginx is a popular open-source web server and reverse proxy server. We can use Nginx as a reverse proxy for Kubernetes services.
Read Full PostPreload data with Cache Function#
The best part of cache function is that you can preload the data as well. You can use cache
function to preload the data. This is helpful when you want to preload the data before the component is rendered.
For example we want only fetch user posts when user data fetched, so we can use cache
function to preload the user posts.
In the above example, we are using cache
function to preload the user posts. So, when the User
component is rendered, the user posts will be already fetched.
This help to improve the performance of the application, as the user posts will be already fetched before the component is rendered.
Calling a memoized function outside of a component will not use the cache. The cache is only used when the memoized function is called inside a component.
Conclusion#
cache
function is useful when we want to memoize work that can be shared across React Server Components. So that we can improve the performance of the application by caching the result or we can preload the data before the component is rendered.
I hope you find this article helpful. If you have any question, feel free to ask in the comment section below. Happy coding!
Comments (3)
Markdown is supported.*0
0
May 8, 2024
1
0
May 4, 2024
nice
9
9
Mar 17, 2024