ZeeCoder / use-resize-observer

A React hook that allows you to use a ResizeObserver to measure an element's size.
MIT License
644 stars 42 forks source link

Defaults are unnecessary code #27

Closed Merri closed 4 years ago

Merri commented 4 years ago

I'm wondering about the defaults, because they seem to provide nothing of value that can't be done with just JavaScript:

const { ref, width = 100, height = 50 } = useResizeObserver()

This gives 100 x 50 until client side code kicks in.

It would make sense to encourage people to use JS features and keep hook's feature set and size as small as possible. (Unless I'm wrong here and I'm simply used to some extra feature not commonly available in most codebases.)

ZeeCoder commented 4 years ago

Yeah when I switched to returning an object instead of an array I didn't consider this possibility, but I think you're right. 🤔

Merri commented 4 years ago

You can do it with arrays, too!

const [ref, width = 100, height = 50] = useOlderResizeObserver()
ZeeCoder commented 4 years ago

Ah, good to know!

ZeeCoder commented 4 years ago

Released. Demo got updates as well here: https://codesandbox.io/s/use-resize-observer-ssr-default-values-hrvt2