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

SyntaxHighlighter cannot be used as a JSX component #539

Open
martinmckenna opened this issue Sep 30, 2023 · 9 comments
Open

SyntaxHighlighter cannot be used as a JSX component #539

martinmckenna opened this issue Sep 30, 2023 · 9 comments

Comments

@martinmckenna
Copy link

martinmckenna commented Sep 30, 2023

Describe the bug

Cannot use Prism as a JSX component in my remix esm project

code:

import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
import { dark } from "react-syntax-highlighter/dist/esm/styles/prism";

....tsx
return (
  <SyntaxHighlighter
    {...(props as any)}
    children={String(children).replace(/\n$/, "")}
    language={match[1]}
    PreTag="div"
    style={{ ...dark, overflowX: "scroll" }}
  />
);
/* package.json */

{
    "react-syntax-highlighter": "15.5.0",
    "@types/react-syntax-highlighter": "^15.5.7",
}
/* tsconfig.json */

{
  "include": ["remix.env.d.ts", "**/*.ts", "**/*.tsx"],
  "compilerOptions": {
    "lib": ["DOM", "DOM.Iterable", "ES2019"],
    "skipLibCheck": true,
    "isolatedModules": true,
    "esModuleInterop": true,
    "jsx": "react-jsx",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "target": "ES2019",
    "strict": true,
    "allowJs": true,
    "forceConsistentCasingInFileNames": true,
    "baseUrl": ".",
    "paths": {
      "app/*": ["./app/*"]
    },

    // Remix takes care of building everything in `remix build`.
    "noEmit": true,
  },
  "exclude": [
    "node_modules",
  ]
}

Screen Shot 2023-09-30 at 12 28 23 PM

Screen Shot 2023-09-30 at 12 29 31 PM

@martinmckenna
Copy link
Author

possibly a react 18 compatibility issue?

@martinmckenna
Copy link
Author

martinmckenna commented Oct 1, 2023

added this to my package.json to force react-syntax-highlighter to use react 18 and now it's working, but you guys should really upgrade:

  "resolutions": {
    "react": "18.2.0",
    "react-dom": "18.2.0",
    "@types/react": "18.2.21",
    "@types/react-dom": "18.2.7"
  },

@302570
Copy link

302570 commented Nov 7, 2023

H

@302570
Copy link

302570 commented Nov 8, 2023

Who caress

ryanrishi added a commit to ryanrishi/ryanrishi.com that referenced this issue Nov 21, 2023
* upgrade dependencies

* wip add contentlayer

* wip update links and head tags

* get next-themes working kinda

* create-react-app for next 14

* idk, remove purgecss?

* fix some invalid HTML

* get projects kinda working

* wip add blog to app

* kinda get projects working

* get blog working

* get projects working

* migrate contact page

* migrate 404

* use built-in to transpile modules

see https://nextjs.org/docs/app/api-reference/next-config-js/transpilePackages

* add tailwind back

* misc cleanup

* fix tailwind content dirs

* add blog post layout

* rm more layouts

* fix projects index

* sort projects by date desc

* tweak project card borders

* pull out project item into its own file since I'll maybe use client at some point

* add music page

* kill last layout

* oops add missing import

* add more metadata

* fix date vs. publishedAt

* fix eslint errors

* fixed some MDX components

* use contentlayer for music

* wip add test page

* use remark-gfm@3 since v4 is still getting rolled out

see remarkjs/remark-gfm#57

* update iframe

* rm gray-matter

* upgrade eslint

* rm @types/react-syntax-highlighter because it was causing issues

see react-syntax-highlighter/react-syntax-highlighter#539

* fix Google Analytics

* fix some loudness wars typescript issues

* first successful `yarn build` 🎉

* fix some stuff around slugs

* i uhhhh alright I guess I'm escaping urls in my paths now

* rm dummy next-14 dir

* get cypress in a runnable state

* kill react-spring / reach

* daaaaang framer motion is cool

* add social links to mobile nav

* use new next ESLint plugin

* clean up dependencies

* run eslint

* fix some blog metadata

* fix more metadata

* use FancyH1 for 404

* fix blog post title heading

* add leading-8 to project and blog post pages

* defer to tailwind typography over custom styles

* fix tailwind content path

* fix some more classnames and typography

* fix project card image heights

* minor tweaks to conform w/ Tailwind typography

* change url

* use [role="dialog"] instead of custom CSS selector

* rm deprecated property

* tell cypress it's ok to continue to NEXT_NOT_FOUND errors

* fix music headings tests

* rm double underline from links

* make github percy quiet

* add sharp

* use custom h1 for project page

* lint

* rm dead code

* fix og:image assertion

* add favicon and apple-icon

* rm unused permalink

* fix project image link in head

* add twitter to metadata

* fix issue where I was only taking a screenshot of every other project

* make dividing line in mobile nav dark theme aware

* wait before taking screenshot of mobile nav

* update browserslist

* mv blog heading into page, not layout
@64json
Copy link

64json commented Dec 1, 2023

added this to my package.json to force react-syntax-highlighter to use react 18 and now it's working, but you guys should really upgrade:

  "resolutions": {
    "react": "18.2.0",
    "react-dom": "18.2.0",
    "@types/react": "18.2.21",
    "@types/react-dom": "18.2.7"
  },

Somehow it didn't work for me, and my temporary workaround was:

import {Prism, SyntaxHighlighterProps} from 'react-syntax-highlighter';

const SyntaxHighlighter = Prism as typeof React.Component<SyntaxHighlighterProps>;

@m11o
Copy link

m11o commented Dec 26, 2023

I had the same issues.
The bellow script was resolved.

import {Prism, SyntaxHighlighterProps} from 'react-syntax-highlighter';
const SyntaxHighlighter = (Prism as any) as React.FC<SyntaxHighlighterProps>;

...tsx
return (
  <SyntaxHighlighter
    {...(props as any)}
    children={String(children).replace(/\n$/, "")}
    language={match[1]}
    PreTag="div"
    style={{ ...dark, overflowX: "scroll" }}
  />
);

@studiosciences
Copy link

studiosciences commented Mar 19, 2024

The issue is that your package manager is not resolving "@types/react": "*" to a consistent version.

I manually deleted the resolution from my yarn.lock and ran yarn install again. The issue went away.

Edit: If you use yarn: yarn dedupe

@Inplex-sys
Copy link

I have tried removing the lock file and I'm still having this issue

{
    "react": "18.2.0",
    "react-dom": "18.2.0",
    "@types/react": "18.2.69",
    "@types/react-dom": "18.2.7"
}

@studiosciences
Copy link

@Inplex-sys It's possible another dependency it polluting the version resolution. You'd have to manually inspect the lock file to see what's going on. All @types/react dependencies should resolve to one version. Looks something like this...

"@types/react@npm:*, @types/react@npm:16 || 17 || 18, @types/react@npm:^18.2.67":
  version: 18.2.67
  resolution: "@types/react@npm:18.2.67"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants