How to import an external library into REACT.JS

Asked

Viewed 42 times

0

I am trying to import an external library directly from the browser in REACT.JS, which would be in HTML:

<script type="text/javascript" src="https://bibliotecahipotetica..."></script>

Could someone help me with this, please ?

  • where are you trying to import?. Imports by html should be done inside the index.html,and not within components. This question has already been answered here and here. It helps you?

1 answer

-1

First you have to install lib using for example npm.

npm install nome_lib

Then in the code use

import RecursoDaLib from 'name_lib';

For example, if you want to add the Rxjs library to React, you should first run the npm install rxjs via command line within the project, then go to the JS file where you want to use the library resources and import, import { of } from 'rxjs';

  • So... that’s the question, the library I’m trying to use is not in NPM / Yarn. I am integrating Pagseguro’s recurring payment API, and this is a specific library of them that they provide in the documentation, I need it to generate a token on top of the credit card data, because the data is not sent directly in the call...

  • Can you tell me which library it is? send me the link etc, maybe I can better help you that way. Sometimes if the package is not published in npm but is using npm to build, you can import it via package.json through the repository URL. See https://docs.npmjs.com/cli/v7/commands/npm-install npm install <git-host>:<git-user>/<repo-name> npm install <git repo url>

  • Good morning Derik! The library is this https://stc.sandbox.pagseguro.uol.com.br/pagseguro/api/v2/checkout/pagseguro.directpayment.js

  • Hello Lucas, sorry I can’t help you, the case you described apparently is to import a simple js file in an React project, the link code you sent is not packaged in a js module. There may be some ways for you to import and use this JS file in your project, see the following link: https://betterprogramming.pub/4-ways-of-adding-external-js-files-in-reactjs-823f85de3668. It contains some shapes that vary by component types (Class or Function) you are using. I hope you can solve

  • Thanks Derik! I’ll take a look....

Browser other questions tagged

You are not signed in. Login or sign up in order to post.