It is recommended to manipulate the dom in react?

I would like to know if it is recommended to manipulate the dom with the document.querySelector() method like we did in vanilla javascript ?

You can mix vanilla js with React. I don’t think there’s any problem with that.

No, it’s not recommended, React uses its own way to update the DOM.

1 Like

ok, so how to access the dom directly as in vanilla js

@dark2020light using refs.
You can read all about it on the official docs:

No, it defeats the point of using React. React allows you to do it via refs, but that’s provided as an escape hatch to use if you can’t do it any other way (for example, applying focus() on an element)

Edit: you already have the logic to render elements right in front of you in the code. You just specify what you want to happen when some event occurs.

What you don’t want to be doing is rendering some HTML with React, grabbing a reference to the element, waiting until it renders, using the ref to select the element using a DOM method like querySelector, then imperatively manipulating the DOM. There’s no point using React if that’s how you’re going to program the UI, just write some HTML

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.