` tag in JSX
9 | - use the `useRef()` hook to get a reference to the corresponding DOM element
10 | - use the `useEffect(..., [])` hook to customize the mathfield on mount
11 |
12 | ## Available Scripts
13 |
14 | In the project directory, you can run:
15 |
16 | ### `npm start`
17 |
18 | Runs the app in the development mode.\
19 | Open [http://localhost:3000](http://localhost:3000) to view it in your browser.
20 |
21 | The page will reload when you make changes.\
22 | You may also see any lint errors in the console.
23 |
24 |
25 | ## Learn More
26 |
27 | You can learn more in the [MathLive documentation](https://cortexjs.io/mathlive/guides/react/).
28 |
29 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "mathlive-react",
3 | "version": "0.1.0",
4 | "private": true,
5 | "dependencies": {
6 | "@testing-library/jest-dom": "^5.16.5",
7 | "@testing-library/react": "^13.4.0",
8 | "@testing-library/user-event": "^13.5.0",
9 | "react": "^18.2.0",
10 | "react-dom": "^18.2.0",
11 | "react-scripts": "5.0.1",
12 | "web-vitals": "^2.1.4"
13 | },
14 | "scripts": {
15 | "start": "react-scripts start",
16 | "build": "react-scripts build",
17 | "test": "react-scripts test",
18 | "eject": "react-scripts eject"
19 | },
20 | "eslintConfig": {
21 | "extends": [
22 | "react-app",
23 | "react-app/jest"
24 | ]
25 | },
26 | "browserslist": {
27 | "production": [
28 | ">0.2%",
29 | "not dead",
30 | "not op_mini all"
31 | ],
32 | "development": [
33 | "last 1 chrome version",
34 | "last 1 firefox version",
35 | "last 1 safari version"
36 | ]
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/App.js:
--------------------------------------------------------------------------------
1 | import './App.css'
2 | import '//unpkg.com/mathlive'
3 | import { useState, useRef, useEffect } from 'react'
4 |
5 | // Use { and } to escape { and } in JSX
6 |
7 | function App() {
8 | const [value, setValue] = useState('\\sqrt{x}')
9 |
10 | // Customize the mathfield when it is mounted
11 | const mf = useRef()
12 | useEffect(() => {
13 | // Read more about customizing the mathfield: https://cortexjs.io/mathlive/guides/customizing/
14 | mf.current.smartFence = true
15 |
16 | // This could be an `onInput` handler, but this is an alternative
17 | mf.current.addEventListener('input', (evt) => {
18 | // When the return key is pressed, play a sound
19 | if (evt.inputType === 'insertLineBreak') {
20 | // The mathfield is available as `evt.target`
21 | // The mathfield can be controlled with `executeCommand`
22 | // Read more: https://cortexjs.io/mathlive/guides/commands/
23 | evt.target.executeCommand('plonk')
24 | }
25 | })
26 | }, [])
27 |
28 | // Update the mathfield when the value changes
29 | useEffect(() => {
30 | mf.current.value = value
31 | }, [value])
32 |
33 | return (
34 |
35 |
MathLive with React
36 | setValue(evt.target.value)}>
37 | {value}
38 |
39 | Value: {value}
40 |
41 | )
42 | }
43 |
44 | export default App
45 |
--------------------------------------------------------------------------------
/public/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
12 |
13 |
17 |
18 |
27 | React App
28 |
29 |
30 |
31 |
32 |
42 |
43 |
44 |
--------------------------------------------------------------------------------