Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import libraries using CDN links into reactjs?

Tags:

reactjs

I have tried:

import 'maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css'; 

but it produces error. How can I import a CDN link into my app.js file?

like image 829
ShocKwav3_ Avatar asked Mar 20 '17 22:03

ShocKwav3_


People also ask

How do I import libraries into React?

Installation: Open a terminal inside your ReactJS project folder and write the following code to install react-script-tag Package. Import 'ScriptTag' component: Import the built-in 'ScriptTag' component from the react-script-tag library at the top of the file where we want to add the script tag.

Is there a CDN for React?

Both React and ReactDOM are available over a CDN. To load a specific version of react and react-dom , replace 18 with the version number.


2 Answers

You can include these lines within your html file:

<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script> 

Or, you can import a local stylesheet file that contains the import instruction. See the example below:

App.js

import './App.scss'; 

App.scss

@import url('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css'); 
like image 169
Rafael Berro Avatar answered Sep 22 '22 18:09

Rafael Berro


You don't bundle CDN stuff inside your JS, that sort of defeats the purpose of having it on CDN :). Had this been JS, I would have asked you to use externals, but for a CSS, you can use https://github.com/jso0/html-webpack-cdn-plugin instead.

like image 26
hazardous Avatar answered Sep 20 '22 18:09

hazardous