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

setValue function in useLocalStorage hook doesn't return the correct previous value #2560

Open
vrubliuk opened this issue May 21, 2024 · 2 comments

Comments

@vrubliuk
Copy link

import { useLocalStorage } from 'react-use';

const Demo = () => {
  const [value, setValue] = useLocalStorage('my-key', 'foo');

  return (
    <div>
      <div>Value: {value}</div>
      <button onClick={() => setValue(prev => {
        // prev value is always the same
      
         return 'bar'
      )}>bar</button>
    </div>
  );
};

What is the expected behavior?
the same behavior as in useState React hook

A little about versions:

  • React: 18.2.0
  • react-use: 17.5.0
@amaury-hanser
Copy link

Same issue here.

setStoredSelectionValue((prevValue) => ({
        ...prevValue,
        expirationDate: (new Date().getTime() + HOUR_TO_MILLISECONDS).toString(),
  }));

It returns:

{
   "expirationDate": "1728384977438",
}

Even if I had set other properties.

@13RTK
Copy link

13RTK commented Oct 9, 2024

import { useLocalStorage } from 'react-use';

const Demo = () => {
  const [value, setValue] = useLocalStorage('my-key', 'foo');

  return (
    <div>
      <div>Value: {value}</div>
      <button onClick={() => setValue(prev => {
        // prev value is always the same
      
         return 'bar'
      )}>bar</button>
    </div>
  );
};

What is the expected behavior? the same behavior as in useState React hook

A little about versions:

  • React: 18.2.0
  • react-use: 17.5.0

I don't understand what's wrong, once the localstorage exists, it will not be initialized as 'foo', it will always be 'bar' at your code

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

No branches or pull requests

3 participants