
怎么在react中写js?
使用插件 react-load-script
如果没有安装可以使用 yarn add react-load-script 或者 npm install react-load-script,看你使用哪个包管理工具。
然后你就可以像使用React组件那样插入JS,如下代码。
import React from 'react';
import Script from 'react-load-script';
class DynamicScriptExample extends React.Component {
constructor(props) {
super(props);
this.state = {
scriptStatus: 'no'
}
}
handleScriptCreate() {
this.setState({ scriptLoaded: false })
}
handleScriptError() {
this.setState({ scriptError: true })
}
handleScriptLoad() {
this.setState({ scriptLoaded: true, scriptStatus: 'yes' })
}
render() {
return (
<>
<Script
url="https://cdn.staticfile.org/jquery/3.3.1/jquery.min.js"
onCreate={this.handleScriptCreate.bind(this)}
onError={this.handleScriptError.bind(this)}
onLoad={this.handleScriptLoad.bind(this)}
/>
<div>动态脚本引入状态:{this.state.scriptStatus}</div>
</>
);
}
}
export default DynamicScriptExample;更多web前端知识,请查阅 HTML中文网 !!
以上就是怎么在react中写js?的详细内容,更多请关注易知道|edz.cc其它相关文章!














