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

88
Visualizações
Updated state not reflecting properly

I have below code where I am checking or unchecking the checkbox based on some conditions, and I came across an issue where I am trying to check the checkbox, and it is failing the first time and from the second time onwards works perfectly.

export const AntdDefaultOverrideInputNumberAdapter = ({
  input: { onChange, value },
  disabled,
  defaultValue,
  formatter,
  ...rest
}) => {
  const [isDefaultValueOverriden, setIsDefaultValueOverriden] = useState(
    !!value && value !== defaultValue
  );

  const handleCheckboxChange = () => {
    const hasOverride = !isDefaultValueOverriden;
    setIsDefaultValueOverriden(hasOverride);
    onChange(hasOverride && !!value ? value : defaultValue);
  };

  const handleOverrideChange = (v) => {
    onChange(v);
  };

  return (
    <Badge
      offset={[0, -6]}
      count={
        <div>
            <Checkbox
              disabled={disabled}
              checked={isDefaultValueOverriden}
              onChange={handleCheckboxChange}
              style={{ zIndex: 10 }}
            />
        </div>
      }
    >
      <InputNumber
        size="small"
        onChange={handleOverrideChange}
        disabled={disabled || !isDefaultValueOverriden}
        style={{ width: 65 }}
        value={isDefaultValueOverriden ? value : defaultValue}
        formatter={formatter}
        {...rest}
      />
    </Badge>
  );
};

I am not sure where I am wrong with the above code, The problem only appears on trying to check the checkbox the first time, and it disappears from the second time onwards..

Could anyone suggest any idea on this? Many thanks in advance!!

I am using the "ANTD" library for the checkbox, and the "value" is an empty string, and the defaultValue is "5"

about 4 years ago · Juan Pablo Isaza
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