[Feature Request] Allow dependency length to change in hooks
See original GitHub issueNot allowing dependency lists to change size limits the usefulness of useMemo
in this particular use case but I imagine there are other similar use cases.
To be clear I am talking about the error triggered here:
For instance, in my app I have a bunch of items and the user can select an unlimited amount of them, in another component I want to compute an expensive derived value based on this selection that is relevant only to this component, a good use case for useMemo
.
However it is not currently possible to use useMemo
and I am forced to compute this derived data outside of this component even though I am only interested in doing so whilst this component is mounted.
I don’t understand why a change in dependency list length cannot be assumed to be a change in the dependencies itself?
I believe this can be implemented by changing the above to:
if (prevDeps.length !== nextDeps.length) {
return false;
}
Issue Analytics
- State:
- Created 4 years ago
- Reactions:18
- Comments:20
Yes I still care about this.
I agree with OP, I also don’t understand why instead of throwing an error
prevDeps.length !== nextDeps.length
can’t just count as a change in the dependencies itself.I want to be able to memoize an array but spreading over the values inside the dependency would throw an error if the array changes in size.
A temporary solution could be to use
JSON.stringify(arr)
inside hook’s deps, but this will only work for serializable data.