use-http
use-http copied to clipboard
Use dequal instead of JSON.stringify for useDeepCallback.
I'd like to offer this as a solution to #168.
I couldn't see a reason for using deep object comparison on the dependencies for makeFetch, and the JSON.stringify comparison made it so any callback functions specified in the options wouldn't use state in their outer environment. Using the built-in useCallback hook seems to maintain the existing behavior, while also fixing the issue.
However, this might just be my ignorance talking, and you actually need deep comparison, in which case, I would like to know how you feel about using an alternative for deep comparison, such as https://www.npmjs.com/package/dequal, similar to how it's used in https://github.com/kentcdodds/use-deep-compare-effect.
useDeepCallback uses JSON.stringify as suggested here. If useCallback is used, it will cause an infinite loop because of object comparison in the dependencies.
Go ahead and try your last suggestion, but when I did this I looked at the source for use-deep-compare-effect which used dequal. If it works, and all the tests pass, then I will merge the PR. :)
I think the scenario of infinite looping would only happen if the function references were changing on every render. But, in this case, they seem to be part of the options, and the options are memoized, so I don't think the function references will change unless the options change. That's just my guess.
I've updated the code to use dequal as a dependency, and all seems to behave as expected.