4 ms·
This is really cool & you have a great website! The one thing I don’t quite understand is why this needs to be a React hook per se - couldn’t it just be a norma
by jacobsimon 7y ago
This is really cool & you have a great website! The one thing I don’t quite understand is why this needs to be a React hook per se - couldn’t it just be a normal library function that you call inside your component?
- Waterluvian 7y agoIf you want to be able to keep a handle on a playing sound so that you can stop it or do other things, you've got state. So this is like an audio version of useState.
- joshwcomeau 7y agoRight, so the reasons this needs to be a hook: - As someone else said, it manages playback state. This is technically managed by a third-party library, but we expose an `isPlaying` boolean which is held in React state - It lazy-loads Howler, the underlying audio dependency. This means the bundle cost is ~1kb instead of 11kb (gzip) - It memoizes all the callback functions to avoid breaking React.memo / PureComponent components