4 ms·
I've been following a Github issue about hook support in Flutter, and I understood React hooks much better after reading that than I ever did before. It is clea
by satvikpendem 6y ago
I've been following a Github issue about hook support in Flutter, and I understood React hooks much better after reading that than I ever did before. It is clear now what exactly the problems hooks solve are, composable life-cycle state reuse. I think that because most people don't really understand why hooks were even needed, they can't wrap their minds around them.
https://github.com/flutter/flutter/issues/51752 https://github.com/flutter/flutter/issues/51752
- Izkata 6y ago...That doesn't clear up a whole lot for me. Keeping in mind I know React but not Flutter (there may be details that make this inaccurate for Flutter): > Reusing a State logic across multiple StatefulWidget is very difficult, as soon as that logic relies on multiple life-cycles. The logic should be inside StatefulWidget, and StatefulWidget reused - so no code duplication or hooking into the parent's lifecycle methods. > A typical example would be the logic of creating a TextEditingController (but also AnimationController, implicit animations, and many more). Looks to me more like TextEditingController should be turned into a stateful component so it can be reused in the render - this lets the framework handle its lifecycle and none of the boilerplate is necessary. I've done this a couple times with variations on AnimationController: It uses React.cloneElement on its children to pass down the appropriate state, and because it was itself a component that simply rendered its children, React handled all the lifecycle stuff within that component with no hooks to the parent. Quick edit: This reply in that issue is very relevant: https://github.com/flutter/flutter/issues/51752#issuecomment-665380355 https://github.com/flutter/flutter/issues/51752#issuecomment... I can here kinda see where it's coming from, but we've never had reason to use hooks like that, so I guess I just don't see it as as much an issue. Also, a very important part of it that some other comments over here seem to not be aware of: > In practical terms, there are a few things here. First, it's worth noting Hooks aren't an "extra" API to React. They're the React API for writing Components at this point. I think I'd agree that as an extra feature they wouldn't be very compelling.
- satvikpendem 6y agoThe problem arises when you want to reuse a lifecycle function in multiple Stateful or even StatelessWidgets. For example, if you go to the bottom of the issue, I make an example where I needed to have an AnimationController with complex initState and dispose methods. With hooks I could encapsulate all of that and make it work with any Widget I wanted, I didn't have to rewrite it each time. There are a lot more examples, it is worth going through the entire ~250 comments, even though it may take some time. I only understood after doing so, not necessarily by just reading the initial post.
- kansface 6y agoThis is exactly correct and a large driver of the problem for me. React Hooks eschew solving the problem I really care about (shared state) in favor of solving the problem I don't care about 99% of the time (shared life-cycle). On top of that, the API is terrible - magical extra args, stale closures over state are baked in, and using a hidden index to track call order so they can't be used in branches or loops!? I have never written a hook correctly on the first try because the behavior is too unpredictable/subtle.
- satvikpendem 6y agoMaybe you'd like the Vue version which doesn't have those problems. I'm not sure how they implement it however.