简体   繁体   English

如何在 React App 中包含外部 JavaScript?

[英]How to include external JavaScript on React App?

I am new to Reactjs.我是 Reactjs 的新手。 Recently I created a simple typing app using reactjs.最近我使用 reactjs 创建了一个简单的打字应用程序。 In fact, I built that App from my previous PHP project.事实上,我从我之前的 PHP 项目构建了该应用程序。 I need to include my custom javascript file to my reactjs app.我需要将我的自定义 javascript 文件包含到我的 reactjs 应用程序中。

My Home.js file is我的 Home.js 文件是

import React from 'react';
import {Link} from 'react-router-dom';

function Home() {
    return (
<form name="post" method="POST">
              <div class="form-group form-row">
                <div class="col-sm-2 col-md-12">
                  <div class="custom-control custom-radio custom-control-inline">
                    <input type="radio" id="customRadio1" class="custom-control-input" onclick="toggleKBMode(event,this)"/>
                    <label class="custom-control-label" for="customRadio4">One<br/></label>
                  </div>
                  <div class="custom-control custom-radio custom-control-inline">
                    <input type="radio" id="customRadio2"class="custom-control-input" onclick="toggleKBMode(event,this)"/>
                    <label class="custom-control-label" for="customRadio1">Two<br/></label>
                  </div>
                  <div class="custom-control custom-radio custom-control-inline">
                    <input type="radio" id="customRadio3"class="custom-control-input" onclick="toggleKBMode(event,this)"/>
                    <label class="custom-control-label" for="customRadio1">Three<br/></label>
                  </div>
                  <div class="custom-control custom-radio custom-control-inline">
                    <input type="radio" id="customRadio4"class="custom-control-input" onclick="toggleKBMode(event,this)"/>
                    <label class="custom-control-label" for="customRadio1">Four<br/></label>
                  </div>
                </div>
              </div>
              <div class="form-group"><textarea name="comment" class="form-control" id="TextArea" onkeydown="toggleKBMode(event)" onkeypress="javascript:convertThis(event)" autofocus=""></textarea></div>
           
                <div class="col-md-9">
                  <button type="submit" class="btn btn-outline-primary" onclick="DFile()" aria-label="Download"><b>Download</b></button>
                  <button type="submit" class="btn btn-secondary" id="Tools" onclick="copyToClipboard()"><b>Clip</b></button>
                </div>
            </form>
);
}

export default Home;

My App.js file is我的 App.js 文件是

import Home from './system/pages/Home';
import {BrowserRouter as Router, Route, Switch} from 'react-router-dom';

function App() {
    let script = document.createElement("script");
    script.src = "/include/js/customTextAreafunction.js";
    //You might have to add other attributes here 
    script.async = true; //So the script load does not block anything
    document.body.appendChild(script);
  return (
    <Router>
    <div className="container">

    <Switch>
    <Router exact path="/">
    <Home/>
    </Router>
    </Switch>

    </div>
    </Router>
  );
}

export default App;

But I want to add this custom javascript file into my project, which is helping for Textarea functioning.但我想将此自定义 javascript 文件添加到我的项目中,这有助于 Textarea 运行。

Custom js file I want to include reactjs App我想包含 reactjs App 的自定义 js 文件

<script src="/include/js/customTextAreafunction.js"></script>

So kindly please help me to fix this.所以请帮助我解决这个问题。 Thanks in advance.提前致谢。

App.js is edited recently but didn't work. App.js 最近被编辑但没有工作。 Kindly please help my project.请帮助我的项目。

If you want to edit your index.html, you can copy/paste your script tags there.如果你想编辑你的 index.html,你可以在那里复制/粘贴你的脚本标签。

Or you can wait for component mount in App.js and fetch it:或者你可以在 App.js 中等待组件挂载并获取它:

let script = document.createElement("script");
script.src = "/include/js/customTextAreafunction.js";
//You might have to add other attributes here 
script.async = true; //So the script load does not block anything
document.body.appendChild(script);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM