Next.js Discord

Discord Forum

Next.js MDX useState Error

Unanswered
Giant panda posted this in #help-forum
Open in Discord
Giant pandaOP
Hey 👋 ,

I'm working with MDX in Next.js, and originally implemented the Remote MDX code recommended in the docs (https://nextjs.org/docs/pages/building-your-application/configuring/mdx#remote-mdx), however as I am using the app directory, this of course did not work, and thus I rewrote it like:

import { serialize } from 'next-mdx-remote/serialize';
import { MDXRemote } from 'next-mdx-remote';
import { readFileSync } from 'fs';

export default async function RemoteMdxPage() {
  const mdxText = readFileSync('./posts/microservice-discord-bots.mdx').toString();

  const mdxSource = await serialize(mdxText);
  return <MDXRemote {...mdxSource} />;
}


As I am only fetching my mdx code locally. However, this is now causing the flllowing error:

Warning: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.
 ⨯ TypeError: Cannot read properties of null (reading 'useState')
    at stringify (<anonymous>)
 ⨯ TypeError: Cannot read properties of null (reading 'useState')
    at stringify (<anonymous>)
digest: "1767135389"


Which strems from this line in the next-mdx-remote package:
    const [isReadyToRender, setIsReadyToRender] = useState(!lazy || typeof window === 'undefined');


Any ideas?

1 Reply