Skip to content

Instantly share code, notes, and snippets.

@boonebgorges
Created September 9, 2016 04:00
Show Gist options
  • Save boonebgorges/d51e11560ed8298249bdcf2c835328b8 to your computer and use it in GitHub Desktop.
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
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