Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我可以将遗留html/js脚本添加到React中吗_Javascript_Html_Reactjs - Fatal编程技术网

Javascript 我可以将遗留html/js脚本添加到React中吗

Javascript 我可以将遗留html/js脚本添加到React中吗,javascript,html,reactjs,Javascript,Html,Reactjs,我对前端开发相当陌生,我有一个react应用程序,希望添加一些以前创建的旧html代码。此html代码引用多个.js文件。我需要对我的react数据做几件事,然后当我完成这些工作后,用户可以选择部分结果,这些结果应该会触发现有的html功能 为此,尝试了以下操作: componentDidMount () { const script = document.createElement("script"); script.src = "/address/of/file.html"

我对前端开发相当陌生,我有一个react应用程序,希望添加一些以前创建的旧html代码。此html代码引用多个.js文件。我需要对我的react数据做几件事,然后当我完成这些工作后,用户可以选择部分结果,这些结果应该会触发现有的html功能

为此,尝试了以下操作:

componentDidMount () {
    const script = document.createElement("script");

    script.src = "/address/of/file.html";
    script.async = true;

    document.body.appendChild(script);
}

此错误会显示以下消息:语法错误:意外标记您可以将react应用程序包含在旧的html中,您只需调用ReactDOM.render,无论您希望应用程序位于何处


确保旧代码(尤其是js)不会干扰React,即DOM不会操纵Approt中的任何内容

如果要使用外部脚本,则不能使用.html扩展名。但是,您可以使用包含页面javascript的.js文件:

componentDidMount () {
    const script = document.createElement("script");

    script.src = "/address/of/file.js";
    script.async = true;

    document.body.appendChild(script);
}
您将能够使用这个js文件来创建您想要的任意多个页面


您正在创建脚本标记并将html放入其中的意外标记。这真的是你想要的吗?@Tholle html文件包含几个.js文件&进行处理,但现在你提到了,也许我应该单独导入这些.js文件,然后自己从html文件中执行这些操作?我知道,如上所述,这样做只是运行文件,所以不确定如何将它们分别放入
componentDidMount () {
    const script = document.createElement("script");

    script.src = "/address/of/file.js";
    script.async = true;

    document.body.appendChild(script);
}