Support for custom equality function
See original GitHub issueIt seems like the library is currently hardcoded to compare the value produced by the selector using Object.is
Sometimes it may be useful to use a different equality comparison such as shallow equal when composing an object. The redux useSelector
API has an extra optional parameter for this purpose, which can be used like
const selectedData = useSelector(selectorReturningObject, shallowEqual)
Would you consider supporting this in use-context-selector?
Issue Analytics
- State:
- Created 3 years ago
- Reactions:9
- Comments:45 (13 by maintainers)
Top Results From Across the Web
Custom Equality Testers - Jasmine Documentation
A custom equality tester is a function that takes two arguments. If the custom equality tester knows how to compare the two items,...
Read more >When should I use a custom equality function in useSelector()?
I'm trying to understand when it is necessary to use a custom equality function in useSelector() . I have a selector that returns...
Read more >How to define value equality for a class or struct - C# ...
Implement the System.IEquatable<T> interface by providing a type-specific Equals method. This is where the actual equivalence comparison is ...
Read more >useCallback/useEffect support custom comparator #14476
I wanted to share a problem I run into very frequently, which I believe would be solved if React supported custom equality functions...
Read more >Sets with a custom equality/uniqueness function « Python ...
There are several ways to achieve that; this recipe presents two classes, FrozenKeyedSet and KeyedSet, that take an additional function key ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi, thanks for opening an issue. ~This might be controversial, but I have a strong belief that react community should prefer using
useCallback
toequalityFn
. In the case of react-redux, it can’t be helped because it had to provide a clear migration path from the HoC API.~ ~I’m pretty convinced about this with the upcominguseMutableSource
. v2 is already implemented in #12, and it requiresuseCallback
. SupportingequalityFn
withuseMutableSource
would be extra work, and I’m suspicious about how to clearly make the implementation Concurrent Mode safe.~~That said, I’m eager to learn the use case that requires
equalityFn
and provide a workaround or even a util function. Do you have an example that we can discuss on?~(I was stupid back then…)
Hopefully this wrapper solution or something similar can be considered as the standard export of the package. I was really hoping to replace Redux with Context API for application state management, however while
use-context-selector
seemed promising in this endeavor, not being able to specify an equality function like you can in Redux is a deal-breaker.The equality function option would be especially useful when replacing Redux with Context since you usually need to grab both values and methods (mutations) from your context object and so referential equality is a clear problem. With Redux you’re doubly covered in this regard, because instead of selecting mutations you dispatch them through a separate mechanism.
Having the wrapper function mentioned in the above comment as a work-around is great, but I’m unsure how stable the solution is.