0

I want to use the library Trumbowyg in my react project. But the problem is the library throws error jQuery is not defined.

I found info that jquery must be somehow exposed to window as global variable. Authors of the library provide example with Webpack but I don't have this in my project.

So I've installed jquery through npm install and tried to import it and set to the window variable like this (it didn't work):

import jquery from 'jquery';
window.$ = window.jQuery = jquery;

How can I setup jquery so that trumbowyg sees it? I've created my project with create-react-app command if it can help you.

4
  • 1
    I would not recommend using a library like that. It wraps jQuery within a react component. There are many WYSIWYG editors for react. Commented Nov 5, 2018 at 17:25
  • 1
    Check this out github.com/jpuri/react-draft-wysiwyg Commented Nov 5, 2018 at 17:25
  • 1
    sounds like a bug on their end. their package.json should include a peerDependency on jquery. you could also simply add it to your DOM directly using the jquery CDN URL. Commented Nov 5, 2018 at 17:33
  • React and jQuery have completely different paradigms and are rarely compatible. If you are forced to use it, you will probably need to use componentDidMount and several hacks in the React side to allow jQuery work without interferences from React. For jQuery importing problem: Webpack but I don't have this in my project: What do you use to pack src files? Commented Nov 5, 2018 at 17:33

1 Answer 1

2

As others commented, you should choose other library which don't use jQuery. But if you still want to use then you can do like the following:

import jquery from 'jquery'
import Trumbowyg from 'react-trumbowyg';
...
componentDidMount() { // hook the jQuery when component is mounted
  window.$ = window.jQuery = jquery;
}
render() {
  return <Trumbowyg id='react-trumbowyg'/>
}

If this still not working, then require the plugin.

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

1 Comment

thanks, I've decided to use another library as others commented

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.