I am using ReactJS and it is not recommended to use JQuery (or vanilla JS) to maniplate the actual DOM, because it may have unpredictable results because of ReactJS using a virtual DOM.
So, if I include pure Bootstrap in my project, could it have the same adverse effect? If so, the use of reactstrap or react-bootstrap would solve this problem or do those libaries also use the bootstrap javascript under the hood?
Thank you,
Michel
The key thing to Avoid is manipulating the DOM for objects (or html DOM elements) that have been created with react, and have states tracked by react. Outside that you are free to use as much jquery or vanilla javascript as you like for DOM manipulation.
This is because, objects rendered with react js tend to work with the state of the object, and attempting to get things to work like toggling classes can cause clashes, if the element state that reactjs remembers has been altered by javascript or jQuery.
For example: What this means is that you can use react to render bootstrap tabs that use bootstrap javascript and jquery,
Then you can activate the tabs after react has rendered them with jquery. You'll have to limit your react to only the render function and avoid setState or update.
using setState or render after the initial render will have to be avoided, if jquery is used to toggle the classes, and react attempts to do the same later, clashes will occur.
In summary you can use both reactjs and JQuery in the same project , but stick to one (either pure react or pure jquery) per element for post render manipulations
Yes it could cause a conflict with React, but it very much depends on what you want to use in Bootstrap? if you just want to use the styling then that will not cause you issues.
Bootstrap JS functions such as showing and hiding divs ( Accordion for example ) might give you issues if React is going to be expecting that div to be in a certain state on the render.
But i have hobby projects that use React and jQuery just fine as long as i am not asking each of the libraries to overlap. Once i have built an application i then try and remove the jQuery from it and move the function into React with the aim to remove jQuery completely.
If you want to expand your answer to provide an example of what you are working on then i'm happy to add to my answer.
Related
https://jquense.github.io/react-widgets/docs/Multiselect/
If you look at the multiselect at this link, and inspect element, when you click into the input you'll see the main div element change classnames from 'rw-popup-container' to 'rw-popup-container rw-slide-transition-exited'. The class 'rw-slide-transition-exited' contains display=none in css which makes the dropdown disappear.
This process of adding/subtracting classnames is extremely snappy and common among various React libraries like MUI/React Bootstrap. You can inspect the source HTML and see they are all doing it. How, exactly, are they doing this? I've looked through the source JS but I can't figure it out. It doesn't appear to be jQuery addClass()/removeClass() and they are doing conditional rendering in React (which is laggy from personal experience).
As you said, this is pretty commong in React libraries (VueJs and Angular libraries as well).
All the modern javascript frameworks have a way to conditionally set the styles of a component, and they just refresh that attribute, there's no need to re-render everything.
Particullary for React, you can unse the "className" proeprty for that, instead of passing an string you can pass a function, and that will dynamically change the classes in the component.
Example:
Using the same example you used, if you go here, you'll see the code for that component.
https://github.com/jquense/react-widgets/blob/f604f9d41652adc29ccd3455bf17997bc001d9ef/packages/react-widgets/src/Multiselect.tsx#L632
(I marked line 632, because that's were the magic happens)
className={cn(className, 'rw-multiselect')}
In there you can see that className is getting a function (since it's between curly brackets it will be evaluated instead of just passing the value).
And if I'm correct, it is using this other library: https://github.com/JedWatson/classnames
which allows you to conditionally set classes.
Hi I do understand theoritically that why using jquery with react is not good for DOM. But can anyone give me a practical example with the code of react alone and react with jquery and show how is it not good?
From my understanding, React use a "virtual DOM" to know the current state of the DOM and update only what is necessary. In short, the shadow DOM is just a giant object stored in memory in which React will check what changed to update the DOM. It is possible because in React, you declare in the render method what you want to display.
Jquery conflict with that since you don't use the render to "declare" what you want to display and it might cause side-effect.
Since bootstrap library modifies the DOM, it may bring unpredictable behaviour (and difficult bugs to fix) when used along with React. Is it suitable to use bootstrap without including bootstrap.js? What are the downsides?
I understand there is 'react-bootstrap' project, I would rather like to stick with just bootstrap and not add another dependency (if possible).
Thanks for your help.
References:
Advantages of using react-bootstrap over bootstrap
Can we use pure Bootstrap with React.js?
Yes, it's absolutely fine. You just can't use the default JavaScript-enabled bits of Bootstrap.
(If you end up using react-bootstrap, it'll have React implementations of those things anyway.)
As an aside, since React only modifies the DOM where it's mounted, you can mix and match React and jQuery/Bootstrap's Javascript (or whatever), so long as you take care not to let the other JavaScript touch the bits of DOM that are managed by React.
most javascript ui components, like jquery ui, bootstrap or kendo ui takes some html element and renders itself dynamically.
Someting like this:
$('#someselect').autocomplete();
I wonder if these frameworks can be effectively used with react.
Let's say, I would like to wrap such component into react component. When component state changes, e.g. SelectedValue, react rerenders the html. but since the html is just some container where the javascript ui components renders itself(e.g autocomplete), it's not very effective.
Can react be effectively used with most javascript ui components or they have to be built with react-like rendering in mind
This article "Working with the Browser" from the react documentation discusses this.
Using operating on the DOM directly using something like jQuery is not considered good practice for a few reasons:
React intentionally discourages using DOM attributes as selectors with the rationale that tracing props down through the component hierarchy is more scalable.
These plugins don't make use of React's event lifecycle. You would have functionality that was dependent on the DOM being fully mutated, and others that were regular react components.
Still, If necessary you can perform operations on the DOM with something like jQuery inside of componentDidMount.
Here's an example of using React with jQuery UI Sortable.
I am new to react.js and I have heard that this js library reacts badly to anything that modifies it's component structure.
Is there any specific procedure to add content into react elements using Jquery. For example, if we want to add content into react's div field, can we directly use Jquery append method to insert text to that div or is there any other way to implement things?.
The idea is that you either use a traditional approach, or you ditch jQuery and use react and this means using the react rendering tree, probably build tasks, client-side router/SPA.
You should not modify the DOM generated by react components from outside it since it maintains an internal state and a virtual DOM that would become out of sync. You either use one ecosystem or another; they are two very different approaches to writing a website.
If you want to introduce react on a small part of the website first, I would suggest to gather all your data with jquery and then transmit the data into the react component.
Roughly
// gather data with jquery or whatever
var data_array = gatherData();
ReactDOM.render(<MyCustomComponent data={data_array}/>, $('#my-react-root')[0])
Rendering changes during runtime can be managed the same way. Just gather the data again and change the state of the react component accordingly.
If you don't want to extract the data or the html is to complex, you can use dangerouslySetInnerHTML to just path a plain text html string.
From the docs:
<div dangerouslySetInnerHTML={{__html: getMarkup()}} />
Best wishes
Andreas