Created
September 9, 2016 04:00
-
-
Save boonebgorges/d51e11560ed8298249bdcf2c835328b8 to your computer and use it in GitHub Desktop.
React component for rendering a chunk of LaTeX to be processed by MathJax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
export default class LaTeX extends Component { | |
componentDidMount() { | |
this.updateTeX() | |
} | |
componentDidUpdate() { | |
this.updateTeX() | |
} | |
updateTeX() { | |
const { mathKey, itemId, isVisible } = this.props | |
if ( ! isVisible ) { | |
return | |
} | |
const cssId = 'latex-' + itemId + '-' + mathKey | |
if ( window.hasOwnProperty( 'MathJax' ) && window.MathJax.hasOwnProperty( 'Hub' ) ) { | |
window.MathJax.Hub.Queue(["Update", window.MathJax.Hub, cssId]); | |
} | |
} | |
shouldComponentUpdate( nextProps ) { | |
return this.props.math !== nextProps.math | |
} | |
render() { | |
const { mathKey, itemId, math, display } = this.props | |
let type = 'math/tex'; | |
if ( 'block' == display ) { | |
type += '; mode=display' | |
} | |
const cssId = 'latex-' + itemId + '-' + mathKey | |
return ( | |
<script | |
type={type} | |
dangerouslySetInnerHTML={{__html: math}} | |
id={cssId} | |
/> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment