简体   繁体   English

如何在 reactjs 组件加载之前加载 javaScript 库和 css 文件

[英]How can javaScript library and css files load before component load in reactjs

How can javaScript library and css files load before reactjs component load. javaScript 库和 css 文件如何在 reactjs 组件加载之前加载。

I am converting dataTable Html template to react component but javaScript library and css files are isn't loaded with components.我正在将数据表 Html 模板转换为反应组件,但 javaScript 库和 css 文件未加载组件。

How can I solved it.我该如何解决它。 Can any one help me?谁能帮我?

You can use these lines at your very first component like app.js which renders first ater index.js(for class based components)您可以在第一个组件中使用这些行,例如 app.js,它首先呈现 ater index.js(对于基于 class 的组件)

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

    script.src = "https://use.typekit.net/foobar.js";
    script.async = true;

    document.body.appendChild(script);
}

And for functional components而对于功能组件

useEffect(() => {
    const script = document.createElement("script");

    script.src = "https://use.typekit.net/foobar.js";
    script.async = true;

    document.body.appendChild(script);
}, []);

You can try to use import [ which js library you want to use ] and then put this syntax at the js file top like as you import react;您可以尝试使用 import [您想使用哪个 js 库] 然后将此语法放在 js 文件顶部,就像您导入 react 一样;

 import $ from 'jquery';

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

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