Syntax highlighting component for React
using the seriously super amazing lowlight by wooorm
Check out a small demo here and see the component in action highlighting the generated test code here
npm install react-syntax-highlighter --save
There are other syntax highlighters for React
out there so why use this one? The biggest reason is that all the others rely on triggering calls in componentDidMount
and componentDidUpdate
to highlight the code block and then insert it in the render function using dangerouslySetInnerHTML
or just manually altering the DOM with native javascript. This utilizes a syntax tree to dynamically build the virtual dom which allows for fine tuned patching of the DOM instead of completely overwriting it on any change, and because of this it is also using more idiomatic React
and allows the use of pure function components brought into React
as of 0.14
One of the biggest pain points for me trying to find a syntax highlighter for my own projects was the need to put a stylesheet tag on my page. I wanted to provide out of the box code styling with my modules without requiring awkward inclusion of another libs stylesheets. The styles in this module are all javascript based, and all styles supported by highlight.js
have been ported!
Currently this modules requires that you build with webpack as it uses dynamic require statements
language
- the language to highlight code in.style
- style object rquired from styles directory. here.import { style } from 'react-syntax-highlighter/styles'
. Will use default if style is not included.children
- the code to highlight.spread props
pass arbitrary props to pre tag wrapping code.
import SyntaxHighlighter from 'react-syntax-highlighter';
import { docco } from 'react-syntax-highlighter/dist/styles';
const Component = () => {
const codeString = '(num) => num + 1';
return <SyntaxHighlighter language='javascript' style={docco}>{codeString}</SyntaxHighlighter>;
}