One of the best and most meaningful features of HTML5 is its introduction of more “semantic” tags, or tags that help define the structure of a webpage. Tags like
section
,
article
,
footer
,
nav
, and
aside
are highly useful for improving the accessibility of a website for visually impaired users. They provide valuable information about your site to any screen readers that are processing it, allowing for easier browsing and less time wasted with repeated or irrelevant information.
Semantic tags improve your site's SEO rankings for the same reason — they help search engines understand what your site is about. And on a more basic level, these tags make it easier for developers to understand what role each element plays on a particular page, increasing maintainability.
So where does semantic HTML fit into the React.js framework?
React requires that any render
function returns a single parent element that contains the rest of the content. Depending on the complexity of your UI, this rule can result in an bulky Matryoshka doll of div
s, div
s, and more div
s. The benefits of semantic HTML can get muddied by just how easy it is to stick another handy, neutral div
in your component tree, especially when (out of habit) you start using div
s in place of more semantically useful tags, like button
or nav
.
Enter React v16.2.0, released in November 2017, which introduced an awesome new feature called Fragments :
Fragments look like empty JSX tags. They let you group a list of children without adding extra nodes to the DOM.
With this addition to the React framework, there’s no reason to bulk up the DOM with more div
tags. There’s a handier, even more neutral tag in town that does exactly what we need.
render() {
return (
<>
<h1>Content title</h1>
<p>Sample content</p>
</>
)
}
Fragments are a way not only to streamline your webpage, but also to get out of the habit of using a div
when another, more specific tag would make sense. Let’s make the internet a more accessible place by remembering that for some consumers, semantic HTML isn’t just "nice to have." It’s essential for navigating your UI.
And hey — no more sifting through div
s to debug your front end, am I right?
Cross-posted on the 20spokes blog