react-jsx-parser icon indicating copy to clipboard operation
react-jsx-parser copied to clipboard

Unexpected token

Open ShahriarKh opened this issue 3 years ago • 1 comments

I'm fetching an article from cms. Inside it, there's a line:

<pre class="wp-block-code"><code>css { color: red }</code></pre>

that breaks the rendering and throws this error SyntaxError: Unexpected token (19:44)

I tried with dangerouslySetInnerHTML and it works.

import JsxParser from "react-jsx-parser";
import css from "./Article.module.scss";

export default function Article({ content }) {
    return (
        <article className={css.article}>

            {/* works */}
            <div dangerouslySetInnerHTML={{ __html:content}} />

            {/* doesn't work */}
            <JsxParser
                jsx={content}
                autoCloseVoidElements
                showWarnings={true}
                renderInWrapper={false}
            />

        </article>
    );
}

Is there a solution for this?

ShahriarKh avatar Aug 17 '22 16:08 ShahriarKh

Your code is not a valid JSX the problem is in { color: red } cause u r using " { " the items between curly braces will interpreted as JSX

look to this issue it may help #112

DevMetwaly avatar Aug 23 '22 18:08 DevMetwaly