所以我试着用Reaction、MathJax和MathJs为一个学校项目展示一些数学知识, 代码如下:
const InputForm = () => {
let latex;
let node;
let inicial;
// MathJax.typesetClear();
let parenthesis = 'keep'
let implicit = 'hide'
//'sqrt(75 / 3) + det([[-1, 2], [3, 1]]) - sin(pi / 4)^2'
const [inputExpressionText, setInputExpressionText, ] = useState(initial);
function initial(){
node = math.parse('sqrt(75 / 3) + det([[-1, 2], [3, 1]]) - sin(pi / 4)^2')
latex = node ? node.toTex({parenthesis: parenthesis, implicit: implicit}) : ''
console.log('Node en estado inicial:', node)
console.log('LaTeX en estado inicial:', latex)
return 'sqrt(75 / 3) + det([[-1, 2], [3, 1]]) - sin(pi / 4)^2';
}
useEffect(() => {
//OnInput();
},[inputExpressionText,])
const OnInput = function () {
console.log(inputExpressionText);
inicial = document.getElementById("input")
node = null;
try {
console.log("antes de node initial", inicial.value);
console.log("antes de node", node);
node = math.parse(inicial.value)
console.log("después de node", node);
}
catch (err) {
console.log("No entro a node = math", )
console.log(err.toString())
}
try {
// export the expression to LaTeX
latex = node ? node.toTex({parenthesis: parenthesis, implicit: implicit}) : ''
console.log('LaTeX expression dentro de funcion:', latex)
// display and re-render the expression
}
catch (err) {
}
console.log("lates en fuera del try-catch",latex)
}
console.log("lates xfinal",latex)
console.log("inputExpressionText ", inputExpressionText)
return (
<table className=' table justify-content-between '>
<tbody>
<tr>
<th>Expression</th>
<td><input type="text" onInput={(e) =>{
setInputExpressionText(e.target.value);
OnInput()
}} id="input" value={inputExpressionText}/></td>
</tr>
<tr>
<th>Pretty print</th>
<td><PrettyInput latex={latex}/></td>
</tr>
<tr>
<th>Result</th>
<td><Result input={node} /></td>
</tr>
</tbody>
</table>
)
}
export default InputForm
问题出在这一部分
try {
// export the expression to LaTeX
latex = node ? node.toTex({parenthesis: parenthesis, implicit: implicit}) : ''
console.log('LaTeX expression inside function:', latex)
// display and re-render the expression
}
catch (err) {
}
console.log("lates en fuera del try-catch",latex)
}
console.log("lates xfinal",latex)
当LaTeX Variale获得函数内部的Tex时, console.log('LaTeX expression inside function:', latex)
进行确认,Console显示相应的Tex值.
但是在OnInput函数之外再次调用时,它显示了未定义的代码中断.
我曾try 在here中使用建议的示例MathJs,将其改编为Reaction组件.
该方法只有在使用VITE时添加LIVE时才有效,但如果重新加载则停止工作,因为它显示未加载DOM.
我真的被困住了