218 points · by danabramov · 7y ago · 65 comments
This is worded a bit weirdly. The fact that there is some indirection and the meat of the `setState` is in the renderer and not in the component doesn't really make `setState` an illusion. A call to `setState` still does exactly what we thought it did.
Similarly folks often imagine `this.state` being the source of truth (and thus Hooks seem too ephemeral).
But both of them are indirections to the same place elsewhere. And state for both of them is kept in the renderer. For classes it’s synced to `this.state` at the last moment so you can read it but `this` is not the source of truth.
Yeah maybe. Even when it's not true it is still a valid way of looking at it I think. In the abstract sense that's what's being modelled here with the API, right? The fact that the internals are different is an implementation detail. Of course, it might be an interesting detail, hence your blogpost.
> Similarly folks often imagine `this.state` being the source of truth (and thus Hooks seem too ephemeral).
I still don't understand why this is though. In some of my projects I actually manage my own local state on the component class (without this.state/setState) and auto `forceRender` when something changes. In those cases my state is super sync and everything works fine. With the additional benefit of my local domain logic being modular: I can read my state after having updated it and it works like expected. So know I can write multiple methods dealing with my local state and compose them freely — like actual OO.
I understand the hooks world is a different story, but in the compat component world it all seemed just fine. Why doesn't react use `this.state` as the source of truth, or update it more aggressively so it at least looks like the source of truth to me? Perf reasons? Is it worth it?
Except it does more bookkeeping behind the scenes to know which state you want (since you're not passing in an identificator). Wonder why they chose that solution, as it's just more magic for really no gain, and it will give you funny bugs if you call it in a loop or in a condition.
https://medium.com/@dan_abramov/making-sense-of-react-hooks-...
(TLDR: that makes custom Hooks very powerful)
React applications using the MVC model don't have this problem. In MVC Components just handle rendering. Stateful logic go into controllers. I think React is getting unnecessarily complicated because of the "everything is a component" model.
I strongly recommend comparing/contrasting this with MVC model. Here's an MVC router that works well with React: https://github.com/edman3d/mvc-router
It is a pity that React was originally designed to be the V in MVC, and then the developer community took it in a different direction.
* Developer relations, on the off chance anyone is unfamiliar with the term. A DevRel team for a developer product like React would be in charge of communicating new features, changes, best practices, etc. to the React developer community, answering technical questions on how to use React, and relaying community feedback to the React team.
Famous DevRel I know and what they've been promoting to great success:
- Dan Abramov, Redux and React.
- Evan You, Vue.js
- Lea Verou, Prism, Bliss and CSS in general.
- "@dhh", Ruby on Rails.
- Sindre Sorhus, npm and indie JS.
Note: I just say they have greatly helped to the success, no that these are their projects.
On the other hand, we all know some projects that succeed despite their founders being really bad at DevRel... but those are so rare and basically just put a lot of efforts in something related but not the same (marketing, first to the market, 10x better technically, etc).
My main focus right now is learning how to get better at this since I'd love to do OSS fulltime. Any tip on how to get there would be greatly appreciated. I'm trying to learn mainly from Dan and Evan since IMHO they are next-level in this specific area :)
PS, since you work for Chrome DevTools, I imagine you are in Google. I think, besides the issues of Angular 1->2, the real reason Angular stayed behind is that there were no highly visible DevRels.
There are definitely some lingering low pri bugs in the issue tracker that would improve the QoL that would really help the dev tools experience.
Example: https://bugs.chromium.org/p/chromium/issues/detail?id=771144
This happens every time. So I just stopped moving the tabs, but if the dev tools are not wide enough to show the (ever increasing) number of tabs, it’s always the 3rd party ones that get pushed off.
Good stuff if you want to know how React work internally.
So basically the fact hooks doesn't support multiple react instances isn't a but but a feature :D?
Vue first though, as it's easier to get started with it and it will get a sense of what minimal setup you should expect. Then try to match that with react.
If you start by following most react tutorials, they will make you begin with something way more complicated that it needs to be and you will get discouragedd.
Both have a "cli" tool. Everybody tell you to start with it. I strongly advise against it, as it's the best way to not understand a thing about what you are doing and make bad decisions.
Find a tutorial that starts with the bare lib (no webpack, no transpilation, nothing but a script tag), for both vue and react. Then when you know how this work, find a minimal webpack based tutorial.
Then when you know those basics, use the cli tool, but immediatly use the eject feature. Read the result to understand the whole shebang.
Once you have done that, you can start any project using the cli without eject, and be confident you know what's going on. It will make you not only very productive, but also will let you take decision based on what you know, and not what other people wants you to know.
Now, I personnaly prefer Vue as I find the lib easier to work with, the community saner and the documentation is one of the best I've read, even including non JS projects. But again, make an educated choice.
Can't agree more with this. There are quite a few reasons I think those starter-kits and tutorials are very harmful especially to new developers. Figuring out how your tsconfig/webpack/babelrcs/state-handlers/etc all fit together should be something that should certainly be done and learned incrementally.
Perhaps the issue is the amount of boilerplate you need to start a simple app. There should be a flask[1] like starter for react/vue apps. Parcel seems like the closest thing but its still not obvious to me why they demo 4 files to get started [2] -- we already dynamically transforming and compiling everything why not just have a single `index.js` with a Index/Main entrypoint?
Is there anything out there to do something as simple as:
import * as React from "react"
import {Html, Body, Title} from "parcel"
const Index = () => {
return (
<Html>
<Title>MySite</Title>
<Body>
<h1>Hello world!</h1>
</Body>
</Html>
)
}
with a `cmd {run,build} index.{jsx,tsx}`The steps are pretty much
npx create-react-app && yarn start and finally yarn build for a production ready build.
There are many other tutorials on the web but the official one has had a lot of work put in to making it seamless and letting you get a feel for what it's like to work with React without throwing too many tangential ideas at you.
I teach React regularly, I tried all methods. The only one that make people click is to make them use a flat html page and React.createElement() manually in it first. On their laptop. Offline. With no other tools than a text editor.
This way, you remove the magic, and when you bring it back, people make good use of it.
Magic is good for power users. It's a bad things as a beginer's tool though.
For work/income I'd choose React as there seem to be more jobs for that. Also in the long term it feels like a pretty solid choice. React has a very big and active community, an outstanding development team, and so many codebases in the world are build on it. React will linger on for quite a while I dare to guess.
IMHO it is not really a React vs Vue thing, you could also go for Ember or Angular. Just pick the one that feels and works good for you and fulfils your personal needs we cannot assess.
I've pushed production Vue and React projects and my personal preference is React. I haven't worked with React Hooks yet but it looks like a game-changer and would make my codebases a lot cleaner. I prefer JSX although Vue also has JSX support, but I've yet to work on a project with complete opt-in for it. React also sets you up for React-Native in some way, in case you're considering targeting native platforms as well.
At the end of the day, don't pick React or Vue for a frontend that doesn't need it. For example, your personal site probably doesn't need a UI library. Then ask what your coworkers feel more comfortable with, if you have any. Finally, check how easy it'll be to integrate with (or migrate to) the existing codebase, if any.
Both libraries and ecosystems are incredible at the moment. You can get a clean and reasonable codebase, performant frontend, and great pace of work with either.
If you do go for the Vue route for professional work, consider donating to Evan's [patreon](https://www.patreon.com/evanyou/overview) or [Open Collective](https://opencollective.com/vuejs). And similarly for [Nuxt](https://opencollective.com/nuxtjs). React et al have venture funding or Facebook behind them for the most part.
Both are good options that nothing serious will come against your way when working on most CRUD apps. If it involves serious computations then you should really evaluate both and select what best fits your usecase.
This is very important. Resources can often be more important than tech.
> Disclaimer: Just like most other posts on this blog, you don’t actually need to know any of that to be productive with React. This post is for those who like to see what’s behind the curtain. Completely optional!
Have you read and understood all of tcp.c? Can we quiz you on the USB protocol?
> Just like in my previous post, you don’t need to know this to be productive in React. I didn’t know this for years. Please don’t turn this into an interview question. In fact, this post is more about JavaScript than it is about React.
https://overreacted.io/how-does-react-tell-a-class-from-a-fu...
Sure, things have changed (e.g. TypeScript is far more accessible today), but if you've been doing React for the past few years, the changes haven't been that drastic, and primarily for the better (e.g. Create React App, that mainly allowed you to forget things, rather than having to learn new things).
Focus is broken so you can't tab between inputs. For example, if you compose forms using web components, you'll lose keyboard control over focus movement in your app.