Any improvements or alternative approaches are welcome!
One alternative approach can be found in the CharlieMcVicker/mathjax-react library.
It may be possible to bundle MathJax with the rest of your JavaScript, which might have the nice consequence of allowing you to import
it instead of using the global MathJax
object. But I found it simpler to include the following at the bottom of my html file; this is the common way to load MathJax.