Skip to content

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

@vrubliuk

Description

@vrubliuk
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions