Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MDX and React instructions don't work for TypeScript projects #7047

Closed
2 tasks done
kaycebasques opened this issue Mar 28, 2022 · 5 comments
Closed
2 tasks done

MDX and React instructions don't work for TypeScript projects #7047

kaycebasques opened this issue Mar 28, 2022 · 5 comments
Labels
documentation The issue is related to the documentation of Docusaurus

Comments

@kaycebasques
Copy link
Contributor

kaycebasques commented Mar 28, 2022

Have you read the Contributing Guidelines on issues?

Description

After following these instructions: https://docusaurus.io/docs/markdown-features/react#importing-components

Docusaurus would tell me that the page crashed and that React is undefined.

I eventually had a hunch that it was because I had initialized my Docusaurus project with TypeScript. Perhaps it's obvious to seasoned TS developers but I'm still new to it.

I think I can take on this documentation update myself but I'm creating this issue in case I get sidetracked.

Self-service

  • I'd be willing to address this documentation request myself.
@kaycebasques kaycebasques added documentation The issue is related to the documentation of Docusaurus status: needs triage This issue has not been triaged by maintainers labels Mar 28, 2022
@Josh-Cena
Copy link
Collaborator

@kaycebasques Do you have a reproduction? If anything goes against the documentation you'd better give something so we can investigate, as I'm sure not everyone's running into this.

@Josh-Cena Josh-Cena added status: needs more information There is not enough information to take action on the issue. and removed status: needs triage This issue has not been triaged by maintainers labels Mar 28, 2022
@kaycebasques
Copy link
Contributor Author

kaycebasques commented Mar 29, 2022

Repro

  1. Generate project with following command as instructed in scaffold project website
npx create-docusaurus@latest my-website classic --typescript
  1. Create Highlight component as instructed in MDX and React
// /src/components/Highlight.js
export default function Highlight({children, color}) {
  return (
    <span
      style={{
        backgroundColor: color,
        borderRadius: '2px',
        color: '#fff',
        padding: '0.2rem',
      }}>
      {children}
    </span>
  );
}

(Note there is no TypeScript information in that guide)

  1. Rename /docs/intro.md to /docs/intro.mdx
  2. Add following code to the top of /docs/intro.mdx (below the front matter) to import and use Highlight component as instructed in MDX and React
---
sidebar_position: 1
---

import Highlight from '@site/src/components/Highlight';

<Highlight color="#25c2a0">Docusaurus green</Highlight>
  1. npm run start

Result

image

Fix

Follow the example of the HomePageFeatures component that is included in the starting code:

  • Rename /src/components/Highlight.js to /src/components/Highlight/index.tsx
  • Add import React from 'react' to the top of index.tsx
  • Add type goodness to index.tsx

(This fix sections summarizes the changes I'm proposing to MDX and React... there should probably be an H3 section after importing components dedicated to the TS workflow)

@Josh-Cena
Copy link
Collaborator

Josh-Cena commented Mar 29, 2022

Hi, you need to import React from "react" whenever you use JSX—this is standard React practice. For some explanation, you can see https://www.chakshunyu.com/blog/why-did-and-dont-you-need-to-import-react/ This has nothing to do with TS, because you have to add that import either in JS or TS.

There are many ways to enforce this, for example, there's the ESLint rule: https://github.com/yannickcr/eslint-plugin-react/blob/HEAD/docs/rules/react-in-jsx-scope.md When you use TS, TS will also warn you about React not being in scope when using JSX. Since this is only about JSX and not about Docusaurus, I will close this.

@Josh-Cena Josh-Cena added closed: question This issue is a user error/misunderstanding. and removed status: needs more information There is not enough information to take action on the issue. documentation The issue is related to the documentation of Docusaurus labels Mar 29, 2022
@kaycebasques
Copy link
Contributor Author

Thanks for taking the extra time to link to those docs and explain. Since I'm new to React I understand I may not be your target audience but perhaps a fix here is to just add the import statement to the top of the /src/components/Highlight.js code in the importing components section to reduce the likelihood of another newbie like me opening this issue again on your repo? Or ellipsis (...) to indicate that the Highlight.js code is incomplete. Happy to put in that PR. But I also understand that sometimes it's best to keep the code samples minimal.

@Josh-Cena
Copy link
Collaborator

Oh, I just realized our docs don't add that import statement 🤦‍♂️ Happy to have a PR that adds import React for that!

@Josh-Cena Josh-Cena added documentation The issue is related to the documentation of Docusaurus and removed closed: question This issue is a user error/misunderstanding. labels Apr 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation The issue is related to the documentation of Docusaurus
Projects
None yet
Development

No branches or pull requests

2 participants