-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(readme): description with createWithEqualityFn #1985
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit a757a25:
|
Size Change: 0 B Total Size: 43.5 kB ℹ️ View Unchanged
|
readme.md
Outdated
@@ -86,9 +86,22 @@ const honey = useBearStore((state) => state.honey) | |||
|
|||
If you want to construct a single object with multiple state-picks inside, similar to redux's mapStateToProps, you can tell zustand that you want the object to be diffed shallowly by passing the `shallow` equality function. | |||
|
|||
To use a custom equality function, you need `createWithEqualityFn` from `zustand/traditional`. It takes the second argument for the default equality function. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel it's a bit confusing — "to use a custom equality function" and then "for the default equality function."
Is the default eq fn a default for that store, but you can still override it later? If so, I suggest:
To use a custom equality function, you need `createWithEqualityFn` from `zustand/traditional`. It takes the second argument for the default equality function. | |
To use a custom equality function that will become a new default for the store, you need `createWithEqualityFn` from `zustand/traditional`. | |
Pass an equality function as a second argument. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion. Let me clarify and maybe you have some other suggestions.
create
(in v5) doesn't allow changing equality function. (It's alwaysObject.is
equivalent.)- To use custom equality function,
createWithEqualityFn
is necessary.- The signature is
const useMyStore = createEqualityFn(..., defaultEqualityFn)
. useMyStore(selector, equalityFn)
can override the default equality function.- If you specify
Object.is
for the second argument of createEqualityFn, it behave the same ascreate
< v4.4.0.
- The signature is
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, so my proposal:
To use a custom equality function, you need `createWithEqualityFn` from `zustand/traditional`. It takes the second argument for the default equality function. | |
To use a custom equality function that will become a new default for the store, you need `createWithEqualityFn` from `zustand/traditional`. | |
Pass a desired equality function as a second argument, like so: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I was concerning is the backward compatibility with previous version. But, I think it's not super important.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for the store
It's not for the store (which is still Objet.is), but it's for the hook.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"To use a custom equality function, you need createWithEqualityFn
instead of create
. Usually, specify Object.is
at the second argument for the default equality function, but it's configurable." How about that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"To use a custom equality function, you need createWithEqualityFn
instead of create
. Usually you want to specify Object.is
as the second argument for the default equality function, but it's configurable."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. I'll update it later.
increasePopulation: () => set((state) => ({ bears: state.bears + 1 })), | ||
removeAllBears: () => set({ bears: 0 }), | ||
}), | ||
Object.is // Specify the default equality function, which can be shallow |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the eq fn is shallow, what is the effect of using 'zustand/shallow'? Isn't the eq fn always shallow then, regardless of it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you do this:
import { createWithEqualityFn } from 'zustand/traditional'
import { shallow } from 'zustand/shallow'
const useMyStore = createWithEqualityFn(..., shallow)
You don't need to specify shallow for useMyStore:
useMyStore(...)
// that 👆 is the same as this 👇
useMyStore(..., shallow)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Object.is // Specify the default equality function, which can be shallow | |
customEqualityFunction // This will become the default equality function, unless you pass another one to `useBearStore` hook. |
And then I'd add a customEqualityFunction
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
customEqualityFunction is not defined anywhere, so I think it's confusing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about defining it, but you said that readme should be minimal, and I agree, so I don't push that hard on this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd explicitly write some example of an actually different equality function than the Object.is
to highlight the use case.
One use case I can think about that better highlights what a real-world scenario could be would be something like:
const useNotificationStore = createWithEqualityFn(set => ({
notifications: []
addNotification: notification => set(state => ({ notifications: [...state.notifications, notification] }))
}),
hashEquality
)
function hashEquality(a, b) {
return JSON.stringify(a.notifications.map(it => it.hash)) === JSON.stringify(b.notifications.map(it => it.hash))
}
provided that notification hash is computed in backend.
readme.md
Outdated
@@ -86,9 +86,22 @@ const honey = useBearStore((state) => state.honey) | |||
|
|||
If you want to construct a single object with multiple state-picks inside, similar to redux's mapStateToProps, you can tell zustand that you want the object to be diffed shallowly by passing the `shallow` equality function. | |||
|
|||
To use a custom equality function, you need `createWithEqualityFn` from `zustand/traditional`. It takes the second argument for the default equality function. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, so my proposal:
To use a custom equality function, you need `createWithEqualityFn` from `zustand/traditional`. It takes the second argument for the default equality function. | |
To use a custom equality function that will become a new default for the store, you need `createWithEqualityFn` from `zustand/traditional`. | |
Pass a desired equality function as a second argument, like so: |
increasePopulation: () => set((state) => ({ bears: state.bears + 1 })), | ||
removeAllBears: () => set({ bears: 0 }), | ||
}), | ||
Object.is // Specify the default equality function, which can be shallow |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Object.is // Specify the default equality function, which can be shallow | |
customEqualityFunction // This will become the default equality function, unless you pass another one to `useBearStore` hook. |
And then I'd add a customEqualityFunction
.
```jsx | ||
import { createWithEqualityFn } from 'zustand/traditional' | ||
import { shallow } from 'zustand/shallow' | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const customEqualityFunction = (a, b) => a.bears === b.bears | |
That would be good, but our strong desire is to keep readme.md as minimal as possible. |
You're right. I still suggest this rephrasal: #1985 (comment) |
Related Issues or Discussions
#1937 (reply in thread)
Summary
Clarify to use
createWithEqualityFn
forshallow
.Check List
yarn run prettier
for formatting code and docs