58

I am trying to use some jQuery UI functionality in my reactJS/Redux application. I've imported both jQuery and jQuery UI using:

npm install jquery jquery-ui

And then I've tried:

import $ from 'jquery';
import jQuery from 'query';
import jQuery-ui from 'jquery-ui';

However jQuery UI does not seem to be imported when I try to do something like:

componentDidMount() {
  $('ol#myList').selectable();
}

I believe the issue is with jQuery UI. What am I doing wrong? How can I get jQuery UI to work with this stack?

Thank you!

9
  • for starters, what do you mean by "doesn't work"? Where are you writing that line? It's likely the element isn't initialized yet so you should be using the componentDidMount callback Commented Feb 7, 2016 at 22:22
  • Can you try to console.log($) after you import $? Commented Feb 7, 2016 at 22:22
  • @ZekeDroid Sorry, it was in componentDidMount(), I edited my question. Commented Feb 7, 2016 at 22:25
  • @AlexPalcuie Please see my edits, I believe the issue is with jQuery UI, the selectable() function doesn't seem to work. Commented Feb 7, 2016 at 22:27
  • 1
    "doesn't seem to work" is not a proper problem description that anyone can troubleshoot from Commented Feb 7, 2016 at 22:28

6 Answers 6

68

I'm successfully using partial import from jquery-ui. I mean import to my module only what I needed from jquery-ui:

import $ from 'jquery';
import 'jquery-ui/themes/base/core.css';
import 'jquery-ui/themes/base/theme.css';
import 'jquery-ui/themes/base/selectable.css';
import 'jquery-ui/ui/core';
import 'jquery-ui/ui/widgets/selectable';

( But take in account that I'm using webpack https://webpack.github.io/, in other environment approach may differ)

Sign up to request clarification or add additional context in comments.

7 Comments

Using SystemJS/jspm, this approach works for me but requires adding '!' to the end of the css imports, e.g. import 'jquery-ui/themes/base/core.css!'
Could you share your webpack.conf with us? I tried that with autocomplete import 'jquery-ui/ui/widgets/autocomplete'; and got (0 , _jQuery2.default)(...).autocomplete is not a in my browser. Also, webpack tells me a warning that there are multiple modules with names that only differ in casing.
Thank you, this is the answer, import core and then the widget you need! @muuvmuuv for me that is exactly what i did and does work : import 'jquery-ui/ui/core'; import 'jquery-ui/ui/widgets/autocomplete';
when using jquery-ui with webpack, any idea why it's not possible to import global functions like Sine, Circ, Elastic, Back, Bounce github.com/jquery/jquery-ui/blob/master/ui/effect.js#L932 ?? when I try to import, these functions are always undefined. I've tried different ways import {Sine} from 'jquery-ui' or import {Sine} from 'jquery-ui/ui/effect' - it is undefined in either case. I'm disappointed. Please advise.
Depending on the version of jQuery, it might be just import 'jquery-ui/selectable';. Check exact path in node_modules/jquery-ui. Otherwise great answer, thanks.
|
17

I first,

npm install jquery-ui-bundle --save

When I need it, I

import 'jquery-ui-bundle';
import 'jquery-ui-bundle/jquery-ui.css';

2 Comments

Just to clarify --save includes the library since it is not being manually edited. If you need to modify anything with custom code you'd use --save-dev
"Bundle" means ready for webpack or other bundle tools. Right? Anyway, this works for me.
16

Add a alias in webpack config:

resolve: {
  alias: {
    'jquery-ui': 'jquery-ui-dist/jquery-ui.js'
  }
}

Save them in package.json:

npm i --save jquery
npm i --save jquery-ui-dist

Then

import $ from 'jquery';
import 'jquery-ui';

It work for me with the last jquery(3.2.1) and jquery-ui(1.12.1).

5 Comments

I can't even begin to understand why they would have jquery-ui and jquery-ui-dist packages. I wasted so much time on this. Thanks for adding this answer!
Thanks, this was helpful. I think you can skip the resolve stage if you replace the last line with: import 'jquery-ui-dist/jquery-ui;
@haz - you often have to change your jquery build. Pulling the entire jQuery lib in everytime is very taxing. I can't recommend enough, using the resolve feature. Otherwise you're hardcoding a specific dist into your code, that will probably change.
Well, not precisely. Skipping the resolve step gives up some syntactic sugar, but not much in this instance. In either case, it will still look for the version of JQuery as handled by npm and your package.json file and contained in your node_modules directory. This will update to the most recent, whenever you do npm update.
I got this error configuration has an unknown property 'alias'
5

component name is jqueryui (no hyphen), use import jqueryui from 'jquery-ui' or simply import 'jquery-ui'

4 Comments

When I try this, I get this error when I try to run: `can't execute file: /pathToRoot/bin/server.js error given was: TypeError: Object function ( w ) { if ( !w.document ) { throw new Error( "jQuery requires a window with a document" ); } return factory( w ); } has no method 'extend' at uuid (/pathToRoot/node_modules/jquery-ui/jquery-ui.js:15:3) at Object.<anonymous> (/pathToRoot/node_modules/jquery-ui/jquery-ui.js:316:3) at Module._compile (module.js:456:26) at Module._extensions..js (module.js:474:10) (See next comment for rest)
at require.extensions.(anonymous function) (/pathToRoot/node_modules/babel-core/lib/api/register/node.js:214:7) at Object._module3.default._extensions.(anonymous function) [as .js] (/pathToRoot/node_modules/require-hacker/babel-transpiled-modules/require hacker.js:250:71) at Module.load (module.js:356:32) at Module._load (module.js:312:12) at Function.module._load (/pathToRoot/node_modules/piping/lib/launcher.js:32:16) at Module.require (module.js:364:17)`
It'd be helpful if you can share a simple project with minimal dependencies to reproduce the issue.
import jqueryui from 'jquery-ui' didn't work for me, but import 'jquery-ui' did
1

Selectable is a plugin in JQuery UI. So we need to improt exact plugin from it. If you need selectable, then it would be like:

import 'jquery-ui/ui/widgets/selectable'

Comments

0

Use craigmichaelmartin's solution from https://stackoverflow.com/a/51271892/1510777.

Short version: Put the code into a file called `leaked-jquery.js' and import that instead of 'jquery'.

import {$,jQuery} from 'jquery';
window.$ = $;
window.jQuery = jQuery;
export default $;
export { jQuery };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.