Next.js Discord

Discord Forum

unstable_layout wrapper function

Answered
Croatian Sheepdog posted this in #help-forum
Open in Discord
Croatian SheepdogOP
Hi there,

lets say i have

import { getUser } from './data';
import { unstable_cache } from 'next/cache';
 
const getCachedUser = unstable_cache(
  async (id) => getUser(id),
  ['my-app-user']
);
 
export default async function Component({ userID }) {
  const user = await getCachedUser(userID);
  ...
}


Do i have to call getCachedUser directly in the component? or can i use it in a wrapper function for example:

const getCachedUser = unstable_cache(
  async (id) => getUser(id),
  ['my-app-user']
);

async function getPrettyUser(userID) {
  const user = await getCachedUser(userID)
  return pretty(user)
}
 
export default async function Component({ userID }) {
  const user = await getPrettyUser(userID);
  ...
}


Thank you in advance!
Answered by joulev
the wrapper function is fine. you can call the cached function anywhere on the server
View full answer

3 Replies