Skip to content

Invalid hook call when using react class component in MDX  #2449

Closed
@Danielku15

Description

@Danielku15

Initial checklist

Affected packages and versions

@mdx-js/react 3.0.1

Link to runnable example

No response

Steps to reproduce

I reproduced this problem faily easily with Docusaurus as explained here:

The answer of Josh shows how to reproduce the probelm using mdx-js loader. They sent me over to you as it seem the root cause sits in mdx-js.

Expected behavior

It should be possible declare React class components in MDX files without errors.

Actual behavior

When you try to declare react components using React.Component in MDX files you end up with an Invalid hook call. error.

  1. Setup an empty Docusuarus with latest dependencies
    1. npx create-docusaurus@latest my-website classic --typescript && npx ncu -u && npm i && npm start
    2. Open https://docusaurus.new/stackblitz-ts
  2. Open the docs/tutorial-basics/markdown-features.mdx
  3. Change the file content to
---
sidebar_position: 4
---

import React from 'react'
export class Test extends React.Component {
	render() {
		return (<div>Title</div>);	
	}
}

<Test />
  1. Open the page in the browser.
  2. Experience the error.

Runtime

Node v20

Package manager

npm v10

OS

Windows

Build and bundle tools

Docusaurus

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions