Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

115
Visualizações
Inputing number between a range in React

I have two input fields of type number, one for minimum value and the other for maximum value. At any given time user can update these values, but the minimum value cannot be greater than maximum value and vice versa. These are my functions which I am calling on 'onChange' event of input:

function minValUpdateHandler(e) {
  setMin((prevVal) => {
  const valInNum = +e.target.value;
  if (valInNum < max) {
    return valInNum;
  }
   return prevVal;
 });
}

function maxValUpdateHandler(e) {
  setMax((prevVal) => {
  const valInNum = +e.target.value;
  if (valInNum > min) {
    return valInNum;
  }
  return prevVal;
 });
}

And this is the codesandbox link for the POC. Please assist me with this.

Thanks

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

the problem was the type of value, the type of value is String so '9' can be bigger than '10' this code can solve the problem

parseInt(e.target.value,10); also, you can not set the min attribute from min input and either max for max input

I suggest using return max-1; instead of return prevVal and return min+1; instead of return prevVal

import "./styles.css";

export default function App() {
  const [min, setMin] = useState(0);
  const [max, setMax] = useState(100);

  function minValUpdateHandler(e) {
    setMin((prevVal) => {
      const valInNum = +parseInt(e.target.value);
      if (valInNum < max) {
        return valInNum;
      }
      return max-1;
    });
  }

  function maxValUpdateHandler(e) {
    setMax((prevVal) => {
      const valInNum = +parseInt(e.target.value);
      if (valInNum > min) {
        return valInNum;
      }
      return min+1;
    });
  }

  return (
    <div className="App">
      <h1>Min Max input</h1>
      <div className="inputContainer">
        <div>
          <label htmlFor="min">Min</label>
          <input
            type="number"
            id="min"
            value={min}
            max={max - 1}
            onChange={minValUpdateHandler}
          />
        </div>
        <span>-</span>
        <div>
          <label htmlFor="max">Max</label>
          <input
            type="number"
            id="max"
            value={max}
            min={min + 1}
            onChange={maxValUpdateHandler}
          />
        </div>
      </div>
    </div>
  );
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda