Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Input Range value updating on double clicks instead of single click

I have created an input of type range using react with typescript. The problem I am facing is when I click on the increase/decrease button I have to click them twice then it will increase/decrease value. Sometimes when the value becomes a hundred the decrease button stop working.

Here is the code:

export default function App() {
  const rangeRef = useRef<HTMLInputElement>(null);
  const [rangeValue, setRangeValue] = useState<number>(0);

  const updateRangeValue = () => {
    const rangeRefference = rangeRef.current;
    if (rangeRefference) {
      const currentSilderLevel: number = (rangeRefference.value as unknown) as number;
      setRangeValue(currentSilderLevel);
    }
  };

  const increaseValue = () => {
    const sliderValue = rangeValue + 10;
    setRangeValue(sliderValue);
  };

  const decreaseValue = () => {
    const sliderValue = rangeValue - 10;
    setRangeValue(sliderValue);
  };

  return (
    <div className="App">
      <button onClick={decreaseValue}>Decrease</button>
      <input
        type="range"
        min="0"
        max="100"
        step="10"
        onChange={updateRangeValue}
        value={rangeValue}
        ref={rangeRef}
      />
      <button onClick={increaseValue}>Increase</button>
    </div>
  );
}

I also created a demo sandbox

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

this happens because your rangerValue goes beyond your 0-100 limit.
and also you do not need to use refs for this. just use useState hook.

I have refactored your code.

import { useState } from "react";
import "./styles.css";

export default function App() {
  const [rangeValue, setRangeValue] = useState<number>(0);

  const increaseValue = () => {
    let sliderValue = Math.min(rangeValue + 10, 100);
    setRangeValue(sliderValue);
  };

  const decreaseValue = () => {
    let sliderValue = Math.max(0, rangeValue - 10);
    setRangeValue(sliderValue);
  };

  const handleChange = (event: React.ChangeEvent) => {
    const target = event.target as HTMLInputElement
    setRangeValue(parseInt(target.value, 10))
  }
  return (
    <div className="App">
      <button onClick={decreaseValue}>Decrease</button>
      <input
        type="range"
        min="0"
        max="100"
        step="10"
        value={rangeValue}
        onChange = {handleChange}
      />
      <button onClick={increaseValue}>Increase</button>
    </div>
  );
}

check it out!

Edit red-cloud-ljg4ut

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading