Synchro
Synchro

Reputation: 1269

React - Digit 0 is not saved to local storage all other digits are kept in sync

I am using react-range and local storage for my project I got a problem which intrigued me as I mentioned earlier I am using react-range the user controls the slider from 0 to 100 it looks like this

enter image description here

My problem is that if the user changes the value using the slider to zero (that is, 0), then the number 0 in the local storage will not be saved, but all other numbers from 1 to 100 work fine and are saved in the local storage

export default (Component) => (props) => {

  const ls = parseInt(window.localStorage.getItem('values'));
  const [someValue, setSomeValue] = useState(ls ? [ls] : [25]);

  useEffect(() => {
    localStorage.setItem('values', someValue);
  });

  return (
    <SideBarContext.Provider value={{ someValue, setSomeValue }}>
      <Component {...props} />
    </SideBarContext.Provider>
  );
};

I assumed that the problem lies in parseInt, which returns a string to an integer, then I looked on the Internet and it says that the digit 0 is also an integer, I can’t understand what the problem is

Upvotes: 0

Views: 123

Answers (1)

Zeno Dalla Valle
Zeno Dalla Valle

Reputation: 919

try this:

const [someValue, setSomeValue] = useState((ls!==null) ? [ls] : [25])

Upvotes: 1

Related Questions