I'm currently experiencing this warning pointing at an unnamed object in a config file, and naming it doesn't resolve the warning. Below is the detailed warning and examples.
Warning:
Anonymous arrow functions cause Fast Refresh to not preserve local component state.
Please add a name to your function, for example:
Before
export default () => <div />;
After
const Named = () => <div />;
export default Named;
Before:
export const = {
apiUrl: process.env.NEXT_PUBLIC_API_URL as string,
commitRef: process.env.NEXT_PUBLIC_VERCEL_GIT_COMMIT_REF as string,
ldSdkKey: process.env.NEXT_PUBLIC_LD_SDK_KEY as string,
imgixBaseUrl: process.env.NEXT_PUBLIC_IMGIX_BASE_URL as string,
imgixApiKey: process.env.NEXT_PUBLIC_IMGIX_API_KEY as string,
imgixResourceID: process.env.NEXT_PUBLIC_IMGIX_RESOURCE_ID as string,
};
Attempted Resolution ./src/config.ts:
const AppConfig = {
apiUrl: process.env.NEXT_PUBLIC_API_URL as string,
commitRef: process.env.NEXT_PUBLIC_VERCEL_GIT_COMMIT_REF as string,
ldSdkKey: process.env.NEXT_PUBLIC_LD_SDK_KEY as string,
imgixBaseUrl: process.env.NEXT_PUBLIC_IMGIX_BASE_URL as string,
imgixApiKey: process.env.NEXT_PUBLIC_IMGIX_API_KEY as string,
imgixResourceID: process.env.NEXT_PUBLIC_IMGIX_RESOURCE_ID as string,
};
export default AppConfig;
Hopefully someone else has experienced this and found a resolution.
Turns out the issue was that our older version of Next.js didn't point towards the correct cause of the warning, but still served the warning. Updating next guided me towards the problem code and I was able to resolve it.