kazooi
February 3, 2022, 8:08pm
1
When I put some code examples inside a <code>
tag in Next JS I got this error message here:
I thought that the <code>
tag is prefenting the browser to render the code which is inside, but this example Code is conflicting an error within Next JS
<code> {mobileMenu && CONTENT} </code>
Can somone let me know how to display code which not gets rendered in Next JS?
Thank you !
jenovs
February 3, 2022, 8:24pm
2
It has nothing to do with Next.js it’s a JSX thing: stuff that’s inside elements in curly braces is treated as JavaScript.
kazooi
February 3, 2022, 8:28pm
3
Is there any way to show up curly braces without an error message?
jenovs
February 3, 2022, 8:35pm
4
For example:
<code>{mobileMenu && CONTENT}</code>
1 Like
I think using template literals and HTML entities are the main choices.
But I would maybe suggest looking for a premade component instead like react-syntax-highlighter if you need to show a lot of code.
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
import { dark } from 'react-syntax-highlighter/dist/esm/styles/prism';
export default function App() {
const codeString = "{mobileMenu && CONTENT}";
return (
<div className="App">
<SyntaxHighlighter language="javascript" style={dark}>
{codeString}
</SyntaxHighlighter>
</div>
);
}
1 Like
kazooi
February 4, 2022, 8:01pm
6
Thank you !!
@lasjorg I guess this is the solution! I would like to make a code selection which I can use to remember syntax and to learn JavaScript, so I actually need it for a lot of code examples.
I installed react-syntax-highlighter npm i react-syntax-highlighter
and I just copied and pasted your code example to show if it works, but I got another error message:
SyntaxError: Unexpected token 'export’
This is my code:
import Layout from '../components/Layout';
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
import { dark } from 'react-syntax-highlighter/dist/esm/styles/prism';
export default function devlab() {
const codeString = '{mobileMenu && CONTENT}';
return (
<>
<Layout title="Education">
<h1>Devlab</h1>
<div className="devlab-container">
<div className="devlab-sidebar">
<div className="devlab-javascript">JavaScript</div>
<div className="devlab-reactjs">React JS</div>
<div className="devlab-threejs">Three JS</div>
<div className="devlab-api-connection">API Connection</div>
</div>
<div className="devlab-content">
<h1>React JS</h1>
<h5>Open a Modal</h5>
<p>To open a modal with react..</p>
<SyntaxHighlighter language="javascript" style={dark}>
{codeString}
</SyntaxHighlighter>
</div>
</div>
</Layout>
</>
);
}
Do you know why this error message shows up and how can I solve it?
Thank you for help!!
kazooi
February 4, 2022, 8:08pm
7
Oh I just recognized that the error occures because of this line of code:
// import { dark } from 'react-syntax-highlighter/dist/esm/styles/prism';
It seems like it can not be imported because it could not be found. Do I need to install another library to get acces to the styles?
Didn’t test it but you can try this.
opened 03:30PM - 13 Dec 19 UTC
closed 07:38PM - 13 Jul 20 UTC
**Describe the bug**
Error appears when I trying to import any react-syntax-hig… hlighter styles in NextJS app.
**To Reproduce**
Steps to reproduce the behavior:
1. Create NextJS app using 'create-next-app'
2. Import any react-syntax-highlighter style.
**Expected behavior**
Working Syntax Highlight.
**Desktop (please complete the following information):**
- OS: ArchLinux
- Browser: Chrome
- Version: 77
**Additional context**
Full error message:
```
Unexpected token export
/home/vista1nik/Documents/es-snippets/node_modules/react-syntax-highlighter/dist/esm/styles/hljs/index.js:1
export { default as a11yDark } from './a11y-dark';
^^^^^^
SyntaxError: Unexpected token export
at Module._compile (internal/modules/cjs/loader.js:718:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:785:10)
at Module.load (internal/modules/cjs/loader.js:641:32)
at Function.Module._load (internal/modules/cjs/loader.js:556:12)
at Module.require (internal/modules/cjs/loader.js:681:19)
at require (internal/modules/cjs/helpers.js:16:16)
at Object.react-syntax-highlighter/dist/esm/styles/hljs (/home/vista1nik/Documents/es-snippets/.next/server/static/development/pages/index.js:2563:18)
at __webpack_require__ (/home/vista1nik/Documents/es-snippets/.next/server/static/development/pages/index.js:23:31)
at Module../pages/index.js (/home/vista1nik/Documents/es-snippets/.next/server/static/development/pages/index.js:2156:104)
at __webpack_require__ (/home/vista1nik/Documents/es-snippets/.next/server/static/development/pages/index.js:23:31)
at Object.4 (/home/vista1nik/Documents/es-snippets/.next/server/static/development/pages/index.js:2320:18)
at __webpack_require__ (/home/vista1nik/Documents/es-snippets/.next/server/static/development/pages/index.js:23:31)
at /home/vista1nik/Documents/es-snippets/.next/server/static/development/pages/index.js:91:18
at Object.<anonymous> (/home/vista1nik/Documents/es-snippets/.next/server/static/development/pages/index.js:94:10)
at Module._compile (internal/modules/cjs/loader.js:774:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:785:10)
```
Edit: I tested it and it works for me. So the import would be.
import { dark } from 'react-syntax-highlighter/dist/cjs/styles/prism';
1 Like
kazooi
February 4, 2022, 8:49pm
9
@lasjorg Thank you, highly aprechiate your help! It is working now
Just one last question:
When I try to display multiple lines of code, just the first line of code gets displayed, but the rest will be ignored. It looks like this in my editor:
This is what I try to show as code:
const [mobileMenu, setMobileMenu] = useState(false);
const toggleMobileMenu = () => {
setMobileMenu(!mobileMenu);
};
I paste everything inside the template literals from const codeString:
export default function devlab() {
const codeString = 'const [mobileMenu, setMobileMenu] = useState(false);';
('const toggleMobileMenu = () => {');
('setMobileMenu(!mobileMenu);');
('{mobileMenu && CONTENT}');
It gets rendered like this:
How can I show the whole code block ?
lasjorg
February 4, 2022, 8:58pm
10
Doesn’t look like your string is correct. Try a template literal instead.
const codeString = `const [mobileMenu, setMobileMenu] = useState(false);
('const toggleMobileMenu = () => {');
('setMobileMenu(!mobileMenu);');
('{mobileMenu && CONTENT}');`;
But I’m not sure why some of the code is inside strings?
1 Like
kazooi
February 4, 2022, 10:05pm
11
That´s it, I just forgot the ` !!
Thank you
system
Closed
August 6, 2022, 10:06am
12
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.