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.

Is it possible to set the default comparison function to shallow?

See original GitHub issue

I recently updated to Zustand 3.6.5. The changed order of .subscribe and the need to use subscribeWithSelector came as a bit of a surprise but once I worked everything out it’s worth it for the improved types.

However, I now have to import shallow from 'zustand/shallow' all over my app. So far there’s nowhere that I don’t want to use shallow comparison. Is there any way to set the default comparison function on the store itself? Or if not, could this feature be added?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:26 (14 by maintainers)

github_iconTop GitHub Comments

5reactions
ehyndscommented, Oct 17, 2022

For anyone looking to use the wrapper approach and are wondering what to replace the deprecated StateSelector with, this works:

const useBearStore = create<State>()( ... );

const useShallowBearStore = <U>(selector: (state: State) => U) => {
  return useBearStore(selector, shallow);
};
4reactions
dai-shicommented, Dec 2, 2021

If this works it’s a nice solution too. However, it’s non-obvious - it’s not something most people would ever think of doing

Oh… I thought it’s very obvious. We could add docs about wrapping with custom hooks. This should be very powerful, not only for this use case.

what is the issue with adding options as the second parameter for create?

It increases bundle size. For enhancing features like this, middleware is the way already provided, or a wrapper like that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How does shallow compare work in react - Stack Overflow
Shallow compare works by checking if two values are equal in case of primitive types like string, numbers and in ...
Read more >
Shallow Compare - React
shallowCompare performs a shallow equality check on the current props and nextProps objects as well as the current state and nextState objects. It...
Read more >
Shallow Comparison vs Deep Comparison in Javascript
You most likely heard terms shallow comparison and deep comparison. ... Now we can simply add default Javascript comparison at the end of...
Read more >
Shallow Comparison vs Deep Comparison in Javascript
Learn the difference between shallow comparison, deep comparison and javascript comparison by building this functions with me from scratch.
Read more >
Default comparisons (since C++20) - cppreference.com
3) Declare the defaulted comparison function as a non-member function. Arguments are passed by value. The three-way comparison function (whether ...
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 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