简体   繁体   中英

React.js: Use Header-Imported Javascript Library

I have built a React.js web app and am trying to use Amplitude Analytics, which has a Javascript SDK here :

The instructions say to include a <script></script> in the <head></head> part of the page. I've added the script in my index.html page, and was wondering how I can use the library in my .jsx files.

This is an event tracking library, and to record an event, I need to call amplitude.getInstance().logEvent('EVENT_IDENTIFIER_HERE'); . However, I'm not sure how to import amplitude so that it becomes available in my Main.jsx or my Page1Component.jsx file?

Thanks!!

It has been suggested to: npm install amplitude/Amplitude-Javascript --save which would install the library from github instead from the npm registry.

Then you can import it in your code:

import amplitude from 'amplitude-js/amplitude.min'

Reference: https://github.com/amplitude/Amplitude-Javascript/issues/40

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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