Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying a block of code in react project

I would like to display code samples of different languages on a website built using react.

Ideally the code sample maintains its formatting and includes line numbers. Best case scenario would be to include syntax highlighting based on the language.

Any suggestions on how to approach this is appreciated.

like image 291
DisplayNameHere Avatar asked Oct 18 '25 14:10

DisplayNameHere


1 Answers

You can use react-syntax-highlighter

npm install react-syntax-highlighter --save
import SyntaxHighlighter from 'react-syntax-highlighter';
import { docco } from 'react-syntax-highlighter/dist/esm/styles/hljs';
const Component = () => {
  const codeString = '(num) => num + 1';
  return (
    <SyntaxHighlighter language="javascript" style={docco}>
      {codeString}
    </SyntaxHighlighter>
  );
};
like image 149
Yoel Avatar answered Oct 20 '25 04:10

Yoel