JavaScript React Basics First Steps in React Render an Element

Kieran Barker
Kieran Barker
14,996 Points

Doesn't this violate the principle of unobtrusive JavaScript?

Previous JavaScript courses taught me that any JavaScript I write should be unobtrusive, meaning that critical parts of the page should not rely on it. Otherwise, on a slow network connection, or with JavaScript disabled, these parts of the page would never be seen.

If we're using React to inject key markup into the page... Doesn't it go directly against the principle of UNOBTRUSIVE JavaScript?

2 Answers

Brendan Whiting
Brendan Whiting
Front End Web Development Techdegree Graduate 84,616 Points

Server side rendering is a way to get around this problem, so at least the first page load is not dependent on JavaScript. But you still might need JavaScript to support other interactions after the page loads.

Unobtrusive JavaScript isn't always possible or necessary. If you know that your users could be using an older browser, or using something like the opera mini browser, you should take this into consideration. The market share of devices without JavaScript is shrinking as browsers get upgraded and hardware devices and infrastructure improve in the developing world. And in a lot of cases, you know who your users are and what browsers they're likely to use. Also, a lot of interactive experiences you really can't do without JavaScript. So the best thing you could do with no JavaScript would be some fallback page that says "sorry, you can't use this feature".

It really does depend on your particular app or site, but it's a heavily opinionated matter. Brendan's response makes it seem like you always need javascript nowadays. However many people don't agree with that and believe that unobtrusive and progressive js is still totally relevant and preferable.