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