question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Best way import jQuery plugins or external client scripts?

See original GitHub issue

My projects comes with quite many external jQuery plugins. And I got troubles to import them into a specific component and make them work with development mode by npm start (server side rendering). My current solutions is to add them all to the bottom of Html.js return func. But, this way increases page load.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:21 (2 by maintainers)

github_iconTop GitHub Comments

15reactions
manhhailuacommented, Jun 21, 2017

@minhnguyenwp : please stop importing jQuery into your component, that will increase the time on bundling process of Webpack and the size of bundle.js/vendor.js file as well. Just add a <script /> tag which has src attribute link to your jquery file and use $ in every components’ componentDidMount functions without re-importing jQuery.

Be aware of eslint alert that '$' is not defined, put a comment /* global $ */ on top of your files or update .eslintrc.js file at globals property to ignore this alert.

Anyway, using jQuery to handle the DOM might cause React DOM tree crash (React indexes all the actual rendered DOMs by it virtual DOM tree). So, please ensure you read these posts before getting your hands dirty with React and jQuery co-operation.

  1. https://facebook.github.io/react/docs/refs-and-the-dom.html
  2. https://facebook.github.io/react/docs/uncontrolled-components.html
  3. https://facebook.github.io/react/docs/integrating-with-other-libraries.html
  4. https://medium.com/@shuvohabib/using-jquery-in-react-component-the-refs-way-969de9aa651f
8reactions
rortegcommented, Mar 22, 2018

Create one component jQuery, example (components/Jquery.js:

import React from 'react'
import jQuery from 'jquery' // Import directly from node-modules
window.jQuery = jQuery

export default () => (jQuery)

In your component you will use a jQuery plugin, example (components/Slider.js):

import React, {Component} from 'react'
import $ from './Jquery' // Import component
import flexSlider from 'flexslider' // Import directly from node-modules

class Slider extends Component {
    componentDidMount() {
         $(".hero-slider").flexslider({
            controlNav: true,
            directionNav: false,
            animation: "fade"
        });
    }

    render = () => (
        <div className="hero hero-slider">
	    <ul className="slides">
	        <li data-bg-image="dummy/slider-1.jpg">
		    <div className="container">
                         <h3 className="slider-subtitle">Your header goes here</h3>
			<h2 className="slider-title">Professional</h2>
			<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ipsa molestiae necessitatibus possimus ducimus facere, error, nostrum. Quos sapiente ducimus maxime odio alias dolor consequuntur, maiores commodi exercitationem veniam. Id, ex?</p>
			<a href="#" className="button large">Read more</a>
		    </div>
		</li>
                 <li data-bg-image="dummy/slider-2.jpg">
		    <div className="container">
		        <h3 className="slider-subtitle">Your header goes here</h3>
			<h2 className="slider-title">Professional</h2>
			<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. In maiores illo eligendi obcaecati reiciendis, vel perspiciatis aliquid esse architecto deleniti asperiores, laboriosam nemo rerum! Ipsam numquam delectus minus iure sit!</p>
			<a href="#" className="button large">Read more</a>
		    </div>
		</li>
		<li data-bg-image="dummy/slider-3.jpg">
		    <div className="container">
			<h3 className="slider-subtitle">Your header goes here</h3>
			<h2 className="slider-title">Professional</h2>
			<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Laboriosam iure, alias error suscipit porro quidem minus, autem repellendus rerum labore corrupti! Quia quas, architecto, quis non pariatur quisquam nisi magnam.</p>
			<a href="#" className="button large">Read more</a>
		    </div>
		</li>
	    </ul>
	</div>
    )
}

export default Slider
Read more comments on GitHub >

github_iconTop Results From Across the Web

What's The Best Way To Include jQuery In A Rails App?
Copy external JavaScript libraries, like the jQuery plugins to the vendor/assets/javascripts folder. Allow the asset pipeline of Rails to combine all of it ......
Read more >
How To Properly Add jQuery Scripts To WordPress (Easy Steps)
1.Enter jQuery Stealth Mode​​ The first way to get around compatibility mode is to get stealthy with your code. For example, if you're...
Read more >
Day 09: Using jQuery Plugin Inside the Client-Side Web Part ...
In this SharePoint Developer Framework video we walk-through the process of including a the accordion jQuery plugin within the client -side ...
Read more >
Building SharePoint Framework Web Parts using jQuery ...
The way loading external scripts should work​​ In order to use jQuery in your Web Part, you would add: import * as jQuery...
Read more >
Use jQuery plugin from asynchronously loaded external script
I understand why - the plugin is being called before the script is loaded. I know a quick and dirty hack is just...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found