我正在尝试从 Brain.js CDN 调用 Brains.js 组件内部,这就是我的代码 App.js 中的内容:
import { useEffect } from 'react';
import logo from './logo.svg';
import './App.css';
function App() {
useScript('https://unpkg.com/brain.js');
function test() {
var net = new brain.recurrent.LSTM()
net.train([
'doe, a deer, a female deer',
'ray, a drop of golden sun',
'me, a name I call myself',
], {
iterations: 1500,
log: details => console.log(details),
errorThresh: 0.011
});
var output = net.run('doe') // ', a deer, a female deer'
const myOutput = document.querySelector('#myOutput');
myOutput.innerHTML = output;
console.log(output)
}
const useScript = url => {
const script = document.createElement('script');
script.src = url;
script.async = true;
console.log("script");
document.body.appendChild(script);
test();
};
return (
<div id="myOutput"></div>
);
}
export default App;
我收到此错误:
ERROR in [eslint]
src\App.js
Line 9:27: 'brain' is not defined no-undef
搜索关键字以了解有关每个错误的更多信息。
test
函数在哪里调用,它是同步的,所以脚本没有完全加载吗?
也许使用异步调用可以避免这个问题:
return (
<div>
<button onClick={test}>Run Test</button>
<div id="myOutput"></div>
</div>
);