3

Hello i have a problem for my project using Sanity v3 and React Syntax Highlighter. When i use Refactor library for showing my code in the browser it works by looking a tutorial in Code Input by Sanity

But when i want to using React Syntax Highlighter and choosing Prism as theme i cannot show it because the error is look like this :

Server Error TypeError: Super expression must either be null or a function

I am using Next.Js 13 & Typescript and this is my code :

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

types: {
    image: SampleImageComponent,
    code: (props: any) => {
        return (
            <SyntaxHighlighter language={props.value.language} style={dark}>
                {props.value.code}
            </SyntaxHighlighter>
        );
    },
},

How can i add React Syntax Highlighter in my project?

2
  • The given code sample does not raise any type error: tsplay.dev/mbKMEW
    – ghybs
    Jan 10, 2023 at 1:48
  • Yup i dont understand because i want to display in <PortableText> Component. So i have to create every type that Sanity allowed to display it whatever i like. If i use <Refactor> component from github.com/rexxars/react-refractor, it works normally. Jan 10, 2023 at 4:57

3 Answers 3

7

I found a separate solution to this issue. For me it was because the syntax highlighter runs client side and Next13 out of the box uses server components if you're using the new app directory.

Try adding 'use client' at the top of the file that is using SyntaxHighlighter.

1

You can also try importing from 'cjs' as oppose to 'esm' as the code has to be executable from the server side (as mentioned above)

Therefore:

import { dark } from "react-syntax-highlighter/dist/esm/styles/prism"

0

The errors raise because it should React.FC types like this :

interface CodeInputProps {
    code: string;
    language: string;
}

const CodeInput: React.FC<CodeInputProps> = (props) => {
    const { code, language } = props;
    return (
        <SyntaxHighlighter language={language} style={tomorrow}>
            {code}
        </SyntaxHighlighter>
    );
};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.