r/reactjs • u/acemarke • Mar 16 '23
Introducing react.dev: the new React docs site!
https://react.dev/blog/2023/03/16/introducing-react-dev156
u/janaagaard Mar 16 '23
I found that the "You Might Not Need an Effect" page was a really helpful resource in understanding how React works.
36
u/prawns_song Mar 17 '23
Iāve been trying to convince some peers to stop using unnecessary effects and Iām so glad thereās official documentation to point to now.
2
16
u/musical_bear Mar 17 '23
This is great. Iām going to be sharing this with my team tomorrow. Iāve been trying to explain some of this, but didnāt have great sources to back up what I was saying.
But I just learned some new things by reading this myself. I had no idea it was considered āokā (in rare situations) to call a state setter right inside of a component function (as long as itās nested in a condition). I would have used an effect for this case myself.
4
u/Aswole Mar 17 '23
Yeah, that part caught me as well. They did show a solution after which required neither, but had I seen the inline state setter in a code review, I would have flagged it.
9
u/Saifadin Mar 17 '23
I habe to admit, that I made some of these mistakes just out of laziness. This is definitely an amazing resource for anyone in any level of experience
5
u/janaagaard Mar 17 '23
So did I. We're still finding old, unnecessary useEffects in our code base. Most of the team has fortunately understood when it's possible to avoid them, so we're catching this is code reviews now, and slowly weeding the remaining ones out.
2
u/Negative-Manner-6978 Mar 17 '23
Great article, full of wisdom, nice find.
1
u/janaagaard Mar 17 '23
The new docs have actually been available for quite a while on beta.reactjs.org. Great to see that they have finally been released.
1
u/beeamie1 Mar 17 '23
Iām rebuilding our funnel right now which used useEffect in every. single. component, multiple times. When I saw this page of the docs I was fully convinced to do it better. Really love the new docs.
38
10
u/cvdubbs Mar 17 '23
No more āAre you sure this is the right tutorial to do? It says beta.ā Conversations with new devs on our team. Thank you guys!!!
5
u/CornTrop Mar 17 '23
What's that installation page? I'm confused. They now do not recommend using React alone?
1
u/gaearon React core team Mar 17 '23
I believe the page explicitly answers your question: https://react.dev/learn/start-a-new-react-project#can-i-use-react-without-a-framework
12
u/neg_ersson Mar 17 '23
It really is strange how they barely acknowledge Vite at the bottom of a hidden collapsible. Someone who wants to build something like Figma or Photoshop (i.e. a "true" SPA) will now be recommended to start with Next or Remix, which is kind of backwards imo.
As your data fetching needs get more complex, you are likely to encounter server-client network waterfalls that make your app feel very slow. As your audience includes more users with poor network conditions and low-end devices, you might need to generate HTML from your components to display content earlyāeither on the server, or during the build time. Changing your setup to run some of your code on the server or during the build can be very tricky.
I swear we've all been collectively duped in to thinking that every app built with React is SEO critical and used by mountain people on poor 3G connections. When did this shift happen? So many apps can afford to be rendered by the client and cached in the browser.
Next and Remix are great but they're not the hammer for every nail.
2
u/gaearon React core team Mar 17 '23
Vite is not barely acknowledged ā it's the primary way we recommend to use React for a part of your page. For new projects, we recommend more integrated solutions.
Someone who wants to build something like Figma or Photoshop (i.e. a "true" SPA) will now be recommended to start with Next or Remix, which is kind of backwards imo.
Why is it backwards? You use client rendering for the client part, and you use static generation for your marketing landing page. Next.js is a hybrid framework, it can produce a regular SPA just fine. But you're not locked out of other options when you need them.
3
u/neg_ersson Mar 18 '23 edited Mar 18 '23
No, it can't create a fully client side SPA (a single document app) without having to ship a Node server with your app. Unless you use something like
next-exportwhich generates a bunch of HTML files at build time and is not the same thing as a SPA (one HTML file).2
u/Wooden_Progress2104 Mar 20 '23
Next is doing exactly what you are asking it to do.
next-exportimplies that you don't want to use Next Router. So if you write multiple page files in the/pagesdirectory, Next gives you multiple pages, exactly as you instructed. If you want to connect those pages together, you write your own router lah, because you explicitly said that you don't want Next Router.If you want one page SPA, you make only one file in the pages directory, and write your app exactly like in vite (that one page has only the App component and the router, e.g. react-router that links to other page components from the
/componentsdirectory). You get exactly what you asked for. It's not Next's fault that you don't understand what you yourself wrote.3
u/neg_ersson Mar 20 '23
What? That is exactly why I'm saying that there is no ideal way to create a fully client-side SPA (that can be hosted on an S3 bucket) using just Next.js and why Vite provides a good intermediate solution for those uses cases.
If you're building a dynamic SPA with Next you'll probably want to run it on a Node.js server (which is totally fine) but you don't need that with a Vite-based SPA. Not every project needs the added complexity and overhead from additional infra.
2
u/gaearon React core team Mar 21 '23
A bunch of HTML files is still an SPA ā just a better one. It behaves like an SPA in all other ways (and is more efficient because it splits code by route). The one trickier part is deployment (need a rewrite map) but itās perfectly possible to host it statically: https://gist.github.com/gaearon/9d6b8eddc7f5e647a054d7b333434ef6
3
u/neg_ersson Mar 21 '23
I get that you and the React team have your reasons to push Vercel products, but a Vite-based SPA will always be easier to host and has more flexible routers for rich applications.
8
15
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...
Is there anyone at the React or JSX team aware of this weakness and oversight?
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
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
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.
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?
0
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
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
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:
Conditional rendering could have components return multiple types of elements or even nothing at all;
Sometimes you'll go through multiple layers of conditional components, adding to the complexity;
Return types of components would need to be very clear about what kind of element they return, e.g.
HTMLDivElementorHTMLButtonElementversusReact.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.
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
Mar 17 '23
[deleted]
1
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
3
2
1
u/imamanv May 17 '24
Although the new documentation is better but in new documentation why they have missed some concepts like code splitting, react portals, render props
1
u/Spirited_Code_8359 May 29 '24
agree. In several places they have information on the legacy site they don't support anymore that they did not include anywhere in the new documentation
1
1
1
1
u/Yuuffy Apr 23 '23
Probably the best documentation I've seen so far. Very well structured & informative with exercises in each section
-1
u/dave__x Mar 17 '23
Awesome page, but how can be reproduced in React this effect of the image when the mouse goes over ?http://www.giphy.com/gifs/CtG3BgOGVDLJmU5QUP
0
Mar 16 '23
[deleted]
79
u/rickhanlonii React core team Mar 16 '23
We owned it already :)
0
Mar 16 '23
[deleted]
12
u/rickhanlonii React core team Mar 16 '23
Thank you!
6
u/andrewsjustin Mar 16 '23
Curious what the sites built on? Next? Remix?
Really great work from what Iāve checked out so far, psyched to have this resource!
24
u/rickhanlonii React core team Mar 17 '23
Itās built on Next with tailwind. Check out the beta directory here: https://github.com/reactjs/reactjs.org
8
-1
u/Aswole Mar 17 '23
Ugh, I don't really feel like arguing with anyone right now about Tailwind, but it's discouraging to me that it's becoming so mainstream that the react docs are now using it. Literally the first element on your page contains some combination of responsive bugs (or at the very least redundant properties):
<div class="h-[40px] hidden lg:flex w-full bg-gray-100 dark:bg-gray-700 text-base md:text-lg py-2 sm:py-0 items-center justify-center flex-col sm:flex-row z-[100]"> //Support Ukraine Content </div>Basically, it is hidden for every viewport except
lg, despite there being explicit styling formdandsmviewports. Which is unfortunate given that the consequence is hiding something designed to promote a good cause. Because tailwind begins to look like hieroglyphics to me once a class has more than a handful of properties, I'll translate it to (s)css and try to understand the intent for each breakpoint:.ukraine-banner { height: 40px; display: hidden; width: 100%; background-color: var(--gray100); font-size: 15px; padding-top: 0.5rem; padding-bottom: 0.5rem; align-items: center; justify-content: center; flex-direction: column; z-index: 100; @media (min-width: 640px) { padding-top: 0rem; padding-bottom: 0rem; flex-direction: row; } @media (min-width: 768px) { font-size: 17px; } @media (min-width: 1024px) { display: flex; } @media (prefers-color-scheme: dark) { background-color: var(--gray700); } }First, it's pretty clear that
display: flexshould at least coincide withflex-direction:rowand thus be moved to thesmbreakpoint (or in tailwind terms,lg:flexshould be changed tosm:flex). What confuses me most is thedisplay: hidden. Was the intent to have this not show up on mobile at all? I'm skeptical, because if so, then thepy-2andflex-colproperties don't make sense as they are immediately overwritten at the first breakpoint where it actually does become visible. My guess ishiddenwas added as a panic fix because someone didn't know how to make it work in mobile, and accidentally broke it for everything butlg. In fact, just removing thehiddenentirely fixes it for all but the smallest viewport (when the text begins to wrap), but changingheight: 40pxtomin-height: 40pxhandles that.3
u/gaearon React core team Mar 17 '23
Which is unfortunate given that the consequence is hiding something designed to promote a good cause.
Yeah, the problem there is that we've had bad random navigation glitches on iOS when we kept it
position: sticky. Its mobile support still isn't great in WebKit. And making it always fixed as a permanent element you can't scroll away takes too much screen estate on mobile. The link is also not very useful on mobile (e.g. facebook mobile site just breaks with it) but it's the only one we could get approved by the lawyers. So I think it made sense to omit.1
u/Aswole Mar 17 '23
I donāt think you should need to make it sticky ā just the nav bar (which already is). At least on my version of iOS, it looks fine on mobile with basic navigation: https://streamable.com/frx4y6
Out of curiosity, was it an issue when the address bar is on the top? Iāve had nightmares wrestling with any sort of fixed elements and how they interact (or more specifically how they donāt interact) with floating browser ui elements across different engines
17
34
u/[deleted] Mar 16 '23 edited Mar 16 '23
looks wonderful. finally one place to look hooks up with all the deeper connotations. but the installation guide. š¶ this must be the most confusing thing for react beginners yet. next, gatsby, remix, i get that cra is unwanted but ... vite? react for the client? spa?