Can I use self-closing tag syntax for any HTML elements I use in JSX?

Question

Can I use self-closing tag syntax for any HTML elements I use in JSX?

Answer

You can! HTML elements we write in JSX can be self-closing as long as we use the forward-slash with right angle bracket to close the element tag.

For example:

<div className="myDiv" />

Now when would this be useful? Making a self-closing element is useful when said element has no children, this should be the only time we use self-closing syntax for HTML elements that are normally not self-closing.

15 Likes

Can I use self-closing tags on components? Or only closing tags?

It’s possible use nested self closing tags?

I’m pretty sure the answer is no, based on my experience with normal html. Since this isn’t normal html though I could be wrong. If you find a way to do it then please let us know.