3 ms·
I have the opposite view. The things you mentioned (componentDidMount, etc.) are actually hard to get right, and class-based components don't help by scattering
by atombender 7y ago
I have the opposite view. The things you mentioned (componentDidMount, etc.) are actually hard to get right, and class-based components don't help by scattering the logic all over the place.
Functional components force your component lifecycle into a funnel where the lifecycle becomes obvious, because all the inter-dependencies are already there.
For componentDidMount, you can replicate this easily with hooks:
const Hello = () => {
const mounted = useRef(false)
useEffect(() => {
if (!mounted.current) {
mounted.current = true
console.log("Mounted!");
}
}, [])
return <>Hello</>
}
Extracting anything into a hook is trivial:
function useMounted(f) {
const mounted = useRef(false)
useEffect(() => {
if (!mounted.current) {
mounted.current = true
f()
}
}, [])
}
const Hello = () => {
useMounted(() => {
console.log("Mounted!");
})
return <>Hello</>
}
Implementing an unmount callback is also trivial.
The useMounted() example, coincidentally, shows a real benefit to hooks: Easy extraction of reusable logic. Hooks are like plugins this way. For example, one hook I've written that I use all the time is this [1], it lets you get a callback every time your component resizes. If you were to do this with a class-based component, you'd have to use an ugly HOC, which results in lots of unnecessary wrapping/nesting.
For componentDidUpdate, useMemo() is arguably easier to deal with.
[1] https://gist.github.com/atombender/b95bbd2014f6a3ec303b843a9bfa0db9 https://gist.github.com/atombender/b95bbd2014f6a3ec303b843a9...
- ArchReaper 7y agoUnless I'm missing something, you don't need all that just to have 'componentDidMount' eg: export const useMounted = (f) => { useEffect(f, []); } Edit: originally: export const useMounted = (f) => { useEffect(() => { f(); }, []); }
- acemarke 7y agoAnd to code-golf it a bit further, it can just be: useEffect(f, [])
- atombender 7y agoAh, of course. The use of dependency lists in React is something I keep forgetting exist. Thanks.