note up front: code only serves as demonstration purpose
So far my understanding goes, DOM manipulation is expensive, so ideally you manipulate the DOM as less as possible. So lets say i want to insert multiply elements, ideally i manipulate the DOM once, for which there are two options, using a string:
i understand the DOM manipulation, my question is: When doing a lot of DOM manipulation (which expensive) how can i do this efficient? taking into consideration: readability, maintainable, performance and amount of code.
ideally, you would do all the manipulation in one go, for which i show two approaches, i need to know which to use and why, concerning the factors i listed above
Ah, I see it’s more complicated than I first thought I guess it depends on how regularly you’d want to change the data. If quickly use the document fragment because it’s easier to quickly navigate a more in depth version. String is easier but less detail. If you know your stuff and confident enough stay with string else the long way with fragment. Hopefully that helped, probably only served in confusing myself, hahah
I think the first one is more efficient/faster. Even faster would be not using jQuery here.
const list = document.querySelector("#category_list");
list.innerHTML = `<div class="category_wrapper">
...
</div>`
If you plan to modify the DOM a lot I’d consider using React.js as it’s best suited for this - and the modules can help a lot with maintenance. Hope this helps
The problem with such a long string is that its difficult to modify and prone to bugs
I never understood ReactJS, its just like Jquery? I need to be able to make AJAX requests as well (to the backend, my django app), how does that work? I looked into ReactJS, but got slightly confused, does ReactJS fit what i need?
I only know about React being fast given it uses a virtualDOM in the form of an object or something, so far my understanding goes
i should really formulate my questions better, after each answer i only ask more questions
It is a Javascript library just like jQuery. Its greatest advantage is quick DOM update due to virtual DOM. It just re renders parts that need to be re rendered.
You’re right about maintenance of long strings like this. If you have a lot to do with them, then React can help you a lot with its JSX components. These are not strings and can help you with splitting your big divs into little reusable components which are easy to maintain. I suggest taking codecademy course on this one
Hello everyone, I am trying to do a simple DOM manipulation in ReactJS, i want the text to be changed when someone clicks on a button, suggestions required from you, help me out with this. Thanks in advance.