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.

10

u/Open_Note Mar 17 '23

Ive noticed warnings in the console about some of these HTML violations when using Next. For example, when nesting p tags. So its definitely possible but doesn't catch every issue atm

1

u/[deleted] Mar 17 '23

SSR solutions also make it easier to validate websites by validating the URL :) If it's client-side rendered, you'd have to copy/paste the HTML from your DOM inspector. It's a hassle.

8

u/JohnMcPineapple Mar 17 '23 edited Oct 08 '24

...

5

u/robrobro Mar 17 '23

While I agree on the basic premise that people write a lot of bad html, I don’t think this should be the concern of react. Use linters and validate the dom using tools for that.

0

u/[deleted] Mar 17 '23

It's a question that I'm unsure about. React uses JSX and the combination should not lead to bad software. That is their responsibility, because: A. They don't pretend to write HTML; and thus: B. They should have validations in place to not make themselves look bad.

Linters don't do a good job, either. The problem is with nested custom components.

<MyLinkToHome active={true}><MySpecialButton isActuallyADiv={false} /></MyLinkToHome>

How would React know what kind of DOM nodes each component wants to create? What if conditional rendering is inside of said components?

My point is that it's very hard to do, and I think it should be the job of React's development team to ensure their product doesn't lead to a large amount of bad websites.

Because, unfortunately, it is part responsible for it.

-1

u/[deleted] Mar 17 '23

The problem is that these don't work, as I said.

2

u/marcocom Mar 17 '23

You’re confusing JSX with the final rendered HTML. It’s just JavaScript that appears like html.

Everything is going to transpired down to a more compatible JS and HTML that you can then lint, should you wish.

0

u/[deleted] Mar 17 '23

The problem is that doing this while writing your code is a nightmare. Yes, you could do it in the browser, but your browser isn't going to crash if you write faulty HTML; it'll do its best to render a working page.

Validating JSX while you're programming your code is very difficult and it's why it basically doesn't exist yet:

  1. Conditional rendering could have components return multiple types of elements or even nothing at all;

  2. Sometimes you'll go through multiple layers of conditional components, adding to the complexity;

  3. Return types of components would need to be very clear about what kind of element they return, e.g. HTMLDivElement or HTMLButtonElement versus React.ReactNode...

See the issue here? If you have hundreds of React nodes in your file, and many of them can be conditional, validating it on the fly is going to be a big performance hit on your system.

Next.js and other such tools could validate the DOM tree when it's being rendered in your browser, and some of them already do that to some extent, but it's incomplete and simply too late.

1

u/Peechez Mar 17 '23

The react-dom reconciler is probably in the best place to validate though honestly. Validating through JSX would be a pain to implement and probably heavy performance hit in your editor. React dom as trivial access to the physical dom though which is what you actually care about. Some dev env warnings wouldn't be the biggest ask I don't think

2

u/robrobro Mar 17 '23

Maybe, but at that point, why not just validate it with a chrome extension or a testing library like Cypress?

2

u/ParadoxScientist Mar 17 '23

Can you explain why the a href example violates HTML rules?

6

u/carlouws Mar 17 '23

Because a button shouldn’t be inside an anchor element. It’s a bit paradoxical. Usually anchor elements are used for page navigations and links while buttons are for actions (usually) that don’t cause navigations (think of the like, follow, subscribe buttons for example). A button that may cause navigation that comes to mind are form submissions.

1

u/ParadoxScientist Mar 17 '23

Ah gotcha, makes sense. Why do people do this then?

I've been learning to code on/off; new-ish to web development.

1

u/funnythrone Mar 17 '23

It's the simplest way to change the mouse cursor on hover, hence used.

0

u/[deleted] Mar 17 '23

[deleted]

1

u/[deleted] Mar 17 '23

It's another anecdotal thing, but the same people who don't understand HTML also don't seem to work well with CSS, and many of them default to Tailwind, and many of those do the combination of React and Tailwind wrong.

React is too forgiving when it comes to writing bad HTML, and my question is mostly about whether it should be responsible for fixing it or not, and if yes: how.

Because it's a very complex problem.

1

u/shiningmatcha Mar 17 '23

Shouldn’t it be doAthing (without the parentheses to invoke the handler)? I’m new to React.

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.

1

u/pm_me_ur_happy_traiI Mar 17 '23

To me it should be a linter concern. You could write wrappers around atomic HTML elements that would limit them to accepting only valid direct children.

But I don't know how you'd be able to prevent illegal children of children like a > span > button