-
Notifications
You must be signed in to change notification settings - Fork 96
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
return memoized object if it equals current value #33
Comments
As workaround, for now I'm using this wrapper: import { useRef } from 'react';
import { equals } from 'ramda';
import _createPersistedState from 'use-persisted-state';
const useMemoizedObject = (obj) => {
const mem = useRef();
if (equals(mem.current, obj)) {
return mem.current;
} else {
mem.current = obj;
return obj;
}
};
const createPersistedState = (key) => {
const usePersistedState = _createPersistedState(key);
return (initialValue) => {
const [_value, setValue] = usePersistedState(initialValue);
const value = useMemoizedObject(_value);
return [value, setValue];
};
};
export default createPersistedState; |
Could you do something like the following for the same result? import createPersistedState from 'use-persisted-state'
import { useMemo } from 'react'
const useLocalStorage = (key: string, initialValue: any) => {
const usePersistedState = createPersistedState(key)
const [_value, setValue] = usePersistedState(initialValue)
const value = useMemo(() => _value, [_value])
return [value, setValue]
}
export default useLocalStorage
|
No, |
I did not know that. Thanks! |
I am working on this (https://github.com/dennismorello/use-persisted-state/tree/feature/deep-comparison). I will open a PR when I'm done 👍🏻 |
morellodev
pushed a commit
to morellodev/use-persisted-state
that referenced
this issue
Nov 9, 2020
morellodev
pushed a commit
to morellodev/use-persisted-state
that referenced
this issue
Nov 9, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Right now this hook can produce unexpected hooks because it will return a different object even if its value is identical to the previous one.
Considered that we are working with JSON data, it would be safe to do a value equality check any time we parse the new value, and if it matches the previous one, we should return the previous one to preserve its identity.
The text was updated successfully, but these errors were encountered: