4 ms·
> > And useState Hook does exactly the same thing. Except it does more bookkeeping behind the scenes to know which state you want (since you're not passing in
by maaaats 8y ago
> > And useState Hook does exactly the same thing.
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.
- danabramov 8y agoI wrote about why here: https://medium.com/@dan_abramov/making-sense-of-react-hooks-fdbde8803889 https://medium.com/@dan_abramov/making-sense-of-react-hooks-... (TLDR: that makes custom Hooks very powerful)
- petilon 8y agoFrom hooks documentation: If you look at a typical React application in React DevTools, you will likely find a “wrapper hell” of components surrounded by layers of providers, consumers, higher-order components, render props, and other abstractions. 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 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.
- danabramov 8y agoDid you read the linked article? I think you’re overfocusing on one argument and might be missing the bigger picture. In my experience MVC doesn’t really help with the problems that extracting and reusing custom Hooks attempts to solve. Controllers aren’t composable in the way functions are. (An alternative take on this is that Hooks are controller-ish.)
- petilon 8y agoFrom the linked article: Components are more powerful, but they have to render some UI. This makes them inconvenient for sharing non-visual logic. This is how we end up with complex patterns like render props and higher-order components. But this is a problem with how some applications are using React, where they make everything a React component. You don't need to make everything a React component. In the MVC model only things that put stuff on the screen need to be React components. You are saying Hooks are better than controllers--and maybe they are. But when the problem is stated as in the above paragraph, then it is not obvious that Hooks are the solution. Maybe the problem should be stated better.