r/reactjs • • Mar 16 '23

Introducing react.dev: the new React docs site!

https://react.dev/blog/2023/03/16/introducing-react-dev
566 Upvotes

74 comments sorted by

View all comments

15

u/[deleted] Mar 16 '23

I have a question regarding React and in particular JSX. Over the years I've noticed a recurring trend (way too often) where developers produce erroneous HTML. It leads to interactive, accessibility, and probably also SEO issues; people break W3C rules constantly. And well, they shouldn't.

JSX isn't HTML, and IDEs (as well as ESLint) don't validate JSX as HTML, not when I last checked anyway.

<a href="/"><button onClick={doAThing()}>home</button></a>

This will be considered perfectly fine pretty much everywhere.

Except it's a violation of the rules of HTML.

Many more issues like it (ul > div > li being one, and wrongly nesting block elements inside inline elements, etc.) take place and developers are none the wiser.

I'm an old guy (nearing my 40s) with 22+ years of experience, I grew up validating the hell out of my work and taking pride in it. To me, it's important; and to the internet, it should be important. The W3C rules are there for reasons; some more important than others.

TL;DR: My questions...

  1. Is there anyone at the React or JSX team aware of this weakness and oversight?

  2. Is this a validating concern (ESLint) or would TypeScript (I know, a different team) be the solution?

I can imagine the TypeScript wizards of the world could take the HTML specs and start going at it, making sure that inside an HTMLAnchorElement you cannot find nested any illegal elements.

Doable, probably. But that also sounds like a performance nightmare.

Personally, I live and breathe the W3C specs and won't make glaring mistakes as I described, but... I've reviewed probably more than a thousand job interview projects and portfolios where a shockingly large number of developers proudly present a webpage with 50 or more HTML spec errors.

Any insights are welcome. It looks like a daunting task, but--based on my anecdotal evidence--it would significantly increase the quality of React websites.

1

u/fixrich Mar 17 '23

Ultimately, as with many things to do with accessibility, I think it’s up to the developer to take initiative to catch these errors. Component based frameworks are terrible for allowing you to create invalid markup because the composition of encapsulation and composition make it easy to do invalid things like in your example. Only today in work I wanted to position some text and an icon on either side of a button. The component library in work has a flex helper component which made it easy. I was fairly certain it used a div so I dug into it to see if I could make it use a span. It was possible but they actually used the typescript omit helper to hide it from the props interface for some reason. I just ignored the error in the end.

I think to be certain you’re presenting valid html on each screen, the most thorough way would be to set up a web driver or puppeteer script that runs the validation on each route you navigate to. You could also run something like axe at the same time. Then you you’d just run it on post build on QA or whatever. I suppose you could just grab all the elements from the browser dev tools and paste them into a validator but I could imagine people not bothering or forgetting.