Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use jQuery plugins in React, Riot 2.0

I have a jQuery heavy app I'm in the middle of that has many jQuery plugins and I want to restructure the app, so I'm looking at different frameworks like Angular, React, Riot 2.0, etc.

I like React and Riot, but I can't figure out how I'd make your typical jQuery app (unmetered access to the DOM) into this new world of virtual DOM, etc.

Some of these components are complex and I wouldn't want to rewrite them all into the "VDOM way" and create another nightmare.

Does anyone have an answer to this? Maybe jQuery plugin heavy apps aren't suited, or is there a way to split/combine the UI using both, or maybe a React/Riot like framework that would play well with jQuery?

like image 384
Timmerz Avatar asked Jan 24 '15 19:01

Timmerz


People also ask

Can React and jQuery be used together?

No. No approach is correct and there is no right way to use both jQuery and React/Angular/Vue together. jQuery manipulates the DOM by, for example, selecting elements and adding/deleting stuff into/from them.

Should I use jQuery in React?

It's a bad practice because React uses a concept called a Virtual DOM instead of a real DOM. And React isn't aware of the changes made outside of this Virtual DOM. When you use jQuery or any other library that manipulates the DOM, React gets confused.

Can we use jQuery in react native?

Nope. JQuery heavily relies on HTML DOM and CSS where react-native has their own DOM-like native view hierarchy with their own style implementation which is similar to subset of CSS (flex-box) but it is implemented differently.


2 Answers

To access the DOM with jQuery inside a Riot 2.0 custom tag, you can use the mount event and this.root like this:

<my-custom-tag>
  <h3>Riot.JS Custom tag + jQuery plugin</h3>
  <p>My paragraph</p>
  <script>
    this.on('mount', function() {
      var $node = $(this.root);
      $node.find('p').html('updated by jQuery!');
    });
  </script>
</my-custom-tag>

I am not sure it is the "best practice" but it works with Riot 2.0.10 (Feb 19, 2015).

If the custom tag contains form or input elements, it's simpler, you can access them by their name property, you don't need the mount event:

<my-custom-form>
  <input name="email" type="text">
  <script>
    $(this.email).val('[email protected]');
  </script>
</my-custom-form>
like image 56
Michael Rambeau Avatar answered Sep 26 '22 17:09

Michael Rambeau


Riot 2.0 was released just 4 days ago so there are obviously no extensions yet.

But it's a good candidate to transform jQuery- based apps to the "modern world" with using custom tags or "components" in React community.

Custom tags lets you build reusable pieces of client side code with a less need for jQuery selectors and manipulation methods. And HTML and JS is less scattered on the file system.

And Riot 2.0 is designed to play well together with jQuery. You can freely use jQuery plugins together with custom tags so you can transform your app iteratively - one tag at the time if you like.

like image 40
Tero Piirainen Avatar answered Sep 26 '22 17:09

Tero Piirainen