When the equality check returns true
return the previous value
#1998
Unanswered
briandipalma
asked this question in
Ideas
Replies: 2 comments 7 replies
-
You can work around this issue with a ref: function getAllFormValues(formID: string) {
return (state: FormState) => {
// A new object reference is created each time this selector is called
return { fieldOne: state.fieldOne.value, fieldTwo: state.fieldTwo.value };
}
}
function useFormValues(formID: string) {
const formValuesRef = useRef({});
const formValues = useFormStore(getAllFormValues(formID));
if (shallow(formValuesRef.current, formValues) === false) {
formValuesRef.current = formValues;
}
return formValuesRef.current;
} |
Beta Was this translation helpful? Give feedback.
1 reply
-
Not sure if I understand it but is it related with #1282 ? |
Beta Was this translation helpful? Give feedback.
6 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
It's great that
shallow
checks prevent a render when you create objects that are the same in a selector but you still end up returning the newest version of the object from the selector. So if another hook causes a render this new object can trigger other hooks if it's used in dependency arrays even though it's the same object in according to the equality function. It would be great if the selector returned the previous object when the equality function says the two values are the same.It looks to me like this a React API change and not something within zustand's control?
Beta Was this translation helpful? Give feedback.
All reactions