Skip to content
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

Open
FezVrasta opened this issue May 15, 2020 · 5 comments
Open

return memoized object if it equals current value #33

FezVrasta opened this issue May 15, 2020 · 5 comments

Comments

@FezVrasta
Copy link

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.

@FezVrasta
Copy link
Author

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;

@natac13
Copy link

natac13 commented Jul 7, 2020

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

@FezVrasta
Copy link
Author

No, useMemo doesn't perform a deep comparison

@natac13
Copy link

natac13 commented Jul 7, 2020

I did not know that. Thanks!

@morellodev
Copy link
Contributor

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 👍🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants