This link says Reactjs is progressive and can be added to a regular web app as follows:
Create a holder for the component:
<div id="root"></div>
Add React script tags to your header:
<script src="https://unpkg.com/react@17/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js" crossorigin></script>
Load the React component:
<script src="mathjax.js"></script>
Create React Component (here is functional component in mathjax.js
):
import React, { useState, useEffect } from 'react';
import { MathComponent } from 'mathjax-react';
const e = React.createElement;
const math_styles = {
"color" : "black",
"display" : "none"
}
function MathJax() {
const [count, setCount] = useState("");
return e(`
<div style={math_styles}>
<MathComponent tex={String.raw`${count}`} />
</div>
`)
}
const domContainer = document.querySelector('#root');
ReactDOM.render(e(LikeButton), domContainer);
export default MathJax;
Notice I added the wrap-around code as suggested in the link:
// at top
const e = React.createElement;
// at bottom
const domContainer = document.querySelector('#root');
ReactDOM.render(e(MathJax), domContainer);
I also tried adding type='module'
to the script tag:
<script type="module" src="mathjax.js"></script>
But I get the following error:
Uncaught SyntaxError: missing ) after argument list
It seems as though the JSX in the React component is causing issues. But the example code in the link above shows encasing the JSX inside a template literal string as I have done.
Technically, the example in the link shows the passing of additional arguments like this:
return e(
'button',
{ onClick: () => this.setState({ liked: true }) },
'Like'
);
But I'm not sure how I would make this work for a functional component like the one in my example.