[英]Loading external script within React (using load-script)
This code is a simple re-implementation react-coin-hive , basically I am trying to understand what is going on here. 这段代码是一个简单的重新实现react-coin-hive ,基本上我正在尝试了解这里发生了什么。
It loads the javascript for Coinhive , however I get the error 它为Coinhive加载了javascript,但是我得到了错误
Line 8: 'CoinHive' is not defined no-undef
Here is the react code: 这是反应代码:
import React, {Component} from 'react';
import loadScript from 'load-script';
class App extends Component {
buildMiner = async () => {
this.miner = await new Promise(resolve => {
loadScript('https://coinhive.com/lib/coinhive.min.js', () => {
return resolve(CoinHive.Anonymous('WshUK1rGzM29IvlWo1qFhk37IgLIh3t3'));
})
})
};
async componentWillMount() {
this.buildMiner();
this.miner.start();
}
render() {
return (
<div>
Start mining!
</div>
);
}
}
export default App;
If you were to load Coinhive normally you would have access to that object, and would call: 如果要正常加载Coinhive,则可以访问该对象,并调用:
<script src="https://coinhive.com/lib/coinhive.min.js"></script>
<script>
var miner = new CoinHive.Anonymous('YOUR_SITE_KEY');
miner.start();
</script>
Another small question I have, is why use the syntax: 我还有另一个小问题,为什么要使用语法:
buildMiner = async () => {}
and not say: 而不是说:
async buildMiner() {}
I would suggest you load the Coinhive library in componentDidMount
, as it is a best practice : Reference , and quote from this site : 我建议您加载Coinhive库componentDidMount
,因为它是一个最佳实践: 参考和引用从这个站点:
If you need to load data from a remote endpoint, this is a good place to instantiate the network request. 如果需要从远程端点加载数据,这是实例化网络请求的好地方。
React being all about props
and state
, why not start with initilazing the state
in a constructor function, like so ? 反应所有关于props
和state
,为什么不像这样在构造函数中初始化state
呢?
constructor(props) {
super(props);
this.state = {
loadScriptCalled: false, // state variable to let us know if loadScript has run
miner: null // state variable to let us know if the miner is available
};
}
Also, since your buildMiner
function returns a Promise
, you do not need to call async
then await
to "promisify" your function. 另外,由于buildMiner
函数返回Promise
,因此您无需调用async
然后await
“承诺”您的函数。
buildMiner = () => {
return new Promise((resolve, reject) => {
loadScript('https://coinhive.com/lib/coinhive.min.js',
(error, script) => {
if (error) {
reject(error);
} else {
console.log("Loaded")
return resolve(CoinHive.Anonymous('WshUK1rGzM29IvlWo1qFhk37IgLIh3t3'));
}
})
})
};
Then, this.buildMiner
being a Promise
, you simply need to treat it as such, and work on your state
component in then()
or catch()
blocks, like so : 然后, this.buildMiner
是Promise
,您只需要这样对待它, then()
在then()
或catch()
块中处理state
组件,如下所示:
componentDidMount() {
this.buildMiner()
.then((miner) => {
console.log("miner :", miner);
this.setState({
loadScriptCalled: true,
miner: miner
});
this.state.miner.start();
})
.catch((error) => {
this.setState({
loadScriptCalled: true
})
console.log("Failed to load CoinHive :", error);
});
}
Complete code is available here : https://codesandbox.io/s/o4lo1my0ky 完整的代码在这里可用: https : //codesandbox.io/s/o4lo1my0ky
Hope this helps ! 希望这可以帮助 !
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.