Since the components are not instantiated, I cannot conceptualize how you are suggesting to use WeakMap here. There is no memory of the last fetch attempt to use as a map key, which is why I opted to loop with an array.

Since fetch can take a Request object as a first parameter and an object as a second parameter, my concern is that the user will not memoize these parameters, instead passing new references each call (useFetch('/path', { method: 'POST' })). The second parameter is a new object that is not strictly equal to the object that was passed the first time this component attempted to mount. Worse, the values on this object can be more deeply-nested new object references. This is why I opted to use the deep-equals package and loop to find the previous object(s) that deep equal despite not being strictly equal.

I would love to improve the performance of the look up. With the user case of useFetch('path', { method: 'POST' }) in mind, I would gladly welcome any pull requests or issues on the GitHub repo. 😊

Senior front end engineer / charlesstover.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store