Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to remove attribute entirely from child in React

I need to remove the attribute "data-processed" from a child div of a component. I cant just set it to null or false, it needs to be removed.

In classic JS, I’d need something like:

$('#mermaid').html(node.data.graph).removeAttr('data-processed');

But that will not seem to work here. I have used useRef to specify the ref to the div, but I cannot find any documentation on how to remove the attribute from the element at the ref. Is this at all possible to do in react?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use vanilla removeAttribute method on the DOM node using useRef. https://developer.mozilla.org/en-US/docs/Web/API/Element/removeAttribute

const domRef = useRef();

domRef.current.removeAttribute('data-processed');
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading