Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
85
rated 0 times [  87] [ 2]  / answers: 1 / hits: 35991  / 6 Years ago, thu, july 12, 2018, 12:00:00

I am trying to work on a project which is using a Layout/Template which uses a lot of jQuery.



I have learned to integrate the template with ReactJS Project, however, I am looking for a solution where I can completely replace the jQuery.



One of my solution is to use jQuery functions inside ComponentDidMount() or Render() function of React.



Is this approach correct? Is it the right way?



I have attached a small example below:



import React, { Component } from 'react';
import '../stylesheets/commonstyles.css';
import '../stylesheets/bootstrap-sidebar.css';
import '../stylesheets/sidebar1.css';
import $ from 'jquery';
class NavBar extends Component {
constructor(props){

super(props);
this.openSidebar = this.openSidebar.bind(this);

}

openSidebar(){

console.log('hello sidebar');

}
componentWillMount(){

$(document).ready(function () {
$('#sidebarCollapse').on('click', function () {
$('#sidebar').toggleClass('active');
});
$('.search-btn').on(click, function () {
$('.search').toggleClass(search-open);
return false;
});

});

}


This is my Render Function.



{/* <!--- SIDEBAR -------> */}
<div class=wrapper style={{paddingTop:60}}>
{/* <!-- Sidebar Holder --> */ }
<nav id=sidebar>
<div class=sidebar-header>
<h3>Dashboard</h3>
<strong>BS</strong>
</div>

<ul class=list-unstyled components>
<li class=active>
<a href=#homeSubmenu /*data-toggle=collapse */ aria-expanded=false>
<i class=ti-home></i>
Home
</a>
<ul class=collapse list-unstyled id=homeSubmenu>
<li><a href=#>Home 1</a></li>
<li><a href=#>Home 2</a></li>
<li><a href=#>Home 3</a></li>
</ul>
</li>
<li>
<a href=# style={{color:white}}>
<i class=ti-align-justify ></i>
About
</a>
<a href=#pageSubmenu /*data-toggle=collapse */ aria-expanded=false style={{color:white}}>
<i class=ti-text></i>
Pages
</a>
<ul class=collapse list-unstyled id=pageSubmenu>
<li><a href=#>Page 1</a></li>
<li><a href=#>Page 2</a></li>
<li><a href=#>Page 3</a></li>
</ul>
</li>
<li>
<a href=# style={{color:white}}>
<i class=ti-paragraph></i>
Portfolio
</a>
</li>
<li>
<a href=# style={{color:white}}>
<i class=ti-control-play></i>
FAQ
</a>
</li>
<li>
<a href=# style={{color:white}}>
<i class=ti-share-alt></i>
Contact
</a>
</li>
</ul>
</nav>

{ /* <!-- Page Content Holder --> */ }
<div id=content>


</div>
</div>

More From » jquery

 Answers
33

Is this approach correct? Is it the right way?



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. Typically, it selects an existing <div> and sets its text.


The other frameworks don't manipulate the DOM; they generate it from data, and regenerate it whenever this data changes (for instance after an Ajax call).


The problem is, jQuery has no clue about React's presence and actions, and React has no clue about jQuery's presence and actions.


This will necessarily lead to a broken application, full of hacks and workarounds, unmaintainable, not to mention that you have to load two libraries instead of one.


For instance, jQuery will select a <button> and add it a .click() listener; but a split second later, React/Angular/Vue might regenerate the DOM and the button in the process, voiding jQuery's .click(). So you'll ask a question on Stackoverflow, wondering why the heck does your .click() not work. You'll end up adding a dirty setTimeout() hack, in order to delay jQuery's click() handler attachment until after React has regenerated your button. It's straight up your highway to hell.


Solution : use jQuery OR (React/Angular/Vue), not both together.


[#53997] Monday, July 9, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kristinsonjab

Total Points: 364
Total Questions: 98
Total Answers: 98

Location: Christmas Island
Member since Mon, Oct 19, 2020
4 Years ago
kristinsonjab questions
Fri, Mar 4, 22, 00:00, 2 Years ago
Fri, Jan 22, 21, 00:00, 3 Years ago
Fri, Aug 14, 20, 00:00, 4 Years ago
;