2 ms·
And the 12 LOC example allows you to change the logic in one place and not two. While more verbose, this does make it harder to introduce bugs by forgetting to
by crtlaltdel 7y ago
And the 12 LOC example allows you to change the logic in one place and not two. While more verbose, this does make it harder to introduce bugs by forgetting to update the logic everywhere it is used (copy/pasted)
- stickfigure 7y agoYes, it's a better pattern, but then the hookophobes would complain that the example artificially inflates the line count by adding a not-strictly-necessary function. I don't think the author is being disingenuous, just trying to keep the examples straightforward. It doesn't matter anyway. With or without the function, the class example compares unfavorably to the hooks example.
- daok 7y agoThe hook functions need the knowledge that the mount and update are doing the same thing. However, the example would be more complete if the mount was doing something different. In that case, not only the class example would be more clear to read with explicit function for mount and update but also would be similar in terms of the line of code. With Hooks, if you want something different you need to do a "trick" which is less obvious than defining `componentDidMount`. I am not against Hooks, but I do not believe the example portrait the situation properly.