question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for custom equality function

See original GitHub issue

It seems like the library is currently hardcoded to compare the value produced by the selector using Object.is

https://github.com/dai-shi/use-context-selector/blob/ba7476affc7cf198c9ed72c2f5db538b3998ac39/src/index.js#L82

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:open
  • Created 3 years ago
  • Reactions:9
  • Comments:45 (13 by maintainers)

github_iconTop GitHub Comments

8reactions
dai-shicommented, Apr 30, 2020

Hi, thanks for opening an issue. ~This might be controversial, but I have a strong belief that react community should prefer using useCallback to equalityFn. 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 upcoming useMutableSource. v2 is already implemented in #12, and it requires useCallback. Supporting equalityFn with useMutableSource 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…)

5reactions
rhyekcommented, Apr 21, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found