1

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"

Youssouf Oumar
  • 29,373
  • 11
  • 46
  • 65
Glory Raj
  • 17,397
  • 27
  • 100
  • 203
  • 1
    It's unclear which is the issue: the initial state, or updating it after? Please clarify. Also, are you trying to make a single component that is both controlled and/or uncontrolled? Is the issue that the input and the badge are not in agreement? The code seems a bit all over the place. Is this basically a duplicate of your previous question [here](https://stackoverflow.com/questions/71118110/check-box-checked-property-is-not-updating-on-initial-check)? Can you provide us a *running* codesandbox demo that reproduces the issue we can inspect and debug live? – Drew Reese Feb 14 '22 at 22:45
  • In the initialState, I have the value as an empty string, and defaultValue is "5", In these circumstances, the checkbox is unchecked, and I am trying to check the checkbox, and in `handleCheckboxChange` I am setting that to true at here `setIsDefaultValueOverriden(hasOverride);` after that state variable is set to false somehow. No, not really of that question – Glory Raj Feb 14 '22 at 22:48
  • I see, so is it because `value` is an empty string and falsey that `isDefaultValueOverriden` is initially false because `!!value && value !== defaultValue` evaluates to false? – Drew Reese Feb 14 '22 at 22:52
  • Yeah, I think somehow the state variable is set to false. But once the value is replaced with 5 – Glory Raj Feb 14 '22 at 22:53
  • Well, if `""` is a valid overridable `value` value then you may want to test explicitly against other falsey value, i.e. `value !== null && value !== undefined && value !== defaultValue`. – Drew Reese Feb 14 '22 at 22:57
  • That is not working either, and I need to replace the value with the default value in the textbox. – Glory Raj Feb 14 '22 at 23:41
  • Can you provide us a *running* codesandbox demo that reproduces the issue that we can inspect and debug. Can you also describe more clearly and completely what the expected behavior should be for this component? It's unclear what you are expecting the code to do or what the use case it. For example... if you need to replace a value with a default value then why are you passing a value to begin with? Why not just use the default value? – Drew Reese Feb 14 '22 at 23:51
  • It's not possible to put this entire code in a sandbox, and this is just an adapter, and I am using this somewhere – Glory Raj Feb 15 '22 at 01:14
  • What do you mean, that's like what, 30 lines of code? You can't put that into a codesandbox with enough supporting code to make it runnable and reproduce any state updating issues? – Drew Reese Feb 15 '22 at 01:15
  • yes, I cannot put that into a sandbox – Glory Raj Feb 15 '22 at 01:17
  • Ok, well cheers and good luck. – Drew Reese Feb 15 '22 at 01:18

0 Answers0