简体   繁体   English

如何在React应用程序中包含第三方js库

[英]How to include third party js libraries in React app

I am new to React and am looking for the React equivalent of this JQuery approach to including analytics throughout my application. 我是React的新手,正在寻找与JQuery方法等效的React,以在整个应用程序中包括分析。

Typically I would: 通常,我会:

  1. Include the 3rd party library on the html pages. 在html页面上包含第3方库。 Easy enough to put on the index.html page but I don't know if that is best practice. 放在index.html页面上很容易,但是我不知道这是否是最佳实践。

    <script src="http://path/to/script/utag.js" />

  2. Then I can interact with the library as long as it has loaded, which I can verify using JQuery window.load. 然后,只要加载了库就可以与它进行交互,可以使用JQuery window.load进行验证。 This script will run fine on a plain html page, but I am trying to find the equivalent best practice way of doing this in my react app. 这个脚本可以在纯HTML页面上正常运行,但是我试图在我的react应用程序中找到等效的最佳实践方法。 I don't want to introduce jquery and currently my React container will tell me that utag is not defined if I try referencing utag in a function. 我不想引入jquery,当前我的React容器会告诉我,如果我尝试在函数中引用utag ,则未定义utag

    <script> $(window).load(function() { utag.link({ "event_name" : "locale_select", "language" : utag_data.language, "currency" : utag_data.currency } ); }); </script>

I'm new to React so any help would be great. 我是React的新手,所以任何帮助都会很棒。 I know that my project is not using webpack, it's using react-scripts and was started using the create-react-app utility. 我知道我的项目没有使用webpack,而是使用react-scripts,并且是使用create-react-app实用程序启动的。

According to this issue on GitHub if you are using create-react-app, if you want to use global variables that imported or created in your index.html file in your react script, you must use window.variable_name. 根据GitHub上的此问题,如果您使用的是create-react-app,则要在react脚本中使用在index.html文件中导入或创建的全局变量,则必须使用window.variable_name。

In your case, this will probably work 在您的情况下,这可能会起作用

import React from "react"
class App extends React.Component {
    componentDidMount() {
        window.utag.link({ "event_name" : "locale_select", "language" : 
    window.utag_data.language, "currency" : window.utag_data.currency } );
    }

    render() {
        return <div />;
    }
}
import someLibrary from 'some-library';

class App extends React.Component {
  componentDidMount() {
    someLibrary();
  }

  render() {
    return <div />;
  }
}

Its important to understand the statement inside the Component offered above. 了解上面提供的组件中的语句很重要。 componentDidMount() is a lifecycle method that gets called automatically after the Component has been rendered to the screen. componentDidMount()是一个生命周期方法,在将组件呈现到屏幕后会自动调用它。 Then inside of it you call your someLibrary(). 然后在其中调用您的someLibrary()。 Depending on what type of third-party library you are talking about will dictate what you may need to also pass into someLibrary(). 根据您所讨论的第三方库的类型,将决定您还需要将哪些内容传递给someLibrary()。

This is how Reactjs interacts with third-party libraries, because typically third-party libraries do not know how to be in a React ecosystem. 这就是Reactjs与第三方库交互的方式,因为通常第三方库不知道如何进入React生态系统。 They don't have any idea what a render() method is or what JSX is. 他们不知道什么是render()方法或什么是JSX。 So this is the general way of making third-party libraries work nicely with React. 因此,这是使第三方库与React良好配合的通用方法。

If you are using create-react-app , then webpack is being used to bundle your javascript. 如果您使用的是create-react-app ,则使用webpack捆绑您的JavaScript。 Here's the documentation on installing dependencies with create-react-app . 这是有关使用create-react-app安装依赖项的文档

To include you library, you should install it as an npm package , and import it into the file where you want to use it. 要包含您的库,您应该将其作为npm软件包安装,并将其导入到要使用它的文件中。 Webpack will include it in the bundle and everything should just work. Webpack将其包含在捆绑包中,一切都应该正常工作。

So, Install the library with npm install some-library . 因此,请使用npm install some-library Import it into a file and call it from a component: 将其导入文件并从组件调用它:

import someLibrary from 'some-library';

class App extends React.Component {
  componentDidMount() {
    someLibrary();
  }

  render() {
    return <div />;
  }
}

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

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