razorpay / blade

Design System that powers Razorpay
https://blade.razorpay.com
MIT License
496 stars 129 forks source link

CheckBoxGroup "defaultValues" prop change not reflecting with re-render #1659

Closed amandal97 closed 12 months ago

amandal97 commented 12 months ago

Please consider the below implementation:

      <CheckboxGroup
        onChange={(e) => {
          props.updateAuthModes(e.values);
        }}
        defaultValue={props.route.auth}
      >
        {props.route.auth.map((authValue, index) => (
          <Checkbox
            key={index}
            size="medium"
            value={authValue}         
          >
            {authValue}
          </Checkbox>
        ))}
      </CheckboxGroup>

Here, when props.route changes, the consequent re-render is not picking up the new values provided to defaultValue prop. This might be because defaultValue is being set as a local state in useControllable hook.

Slack Thread

https://github.com/razorpay/blade/assets/45780866/73f1982a-9e1d-41cf-8a1a-4ae1a3257780

anuraghazra commented 12 months ago

@amandal97 thanks for the issue, will get this fixed. if you are blocked, for now you can add a key prop to the CheckboxGroup to force rerender it.

<CheckboxGroup key={props.route.auth} \/>

anuraghazra commented 12 months ago

when props.route changes, the consequent re-render is not picking up the new values provided to defaultValue prop.

@amandal97 actually this is an expected behaviour of uncontrolled components.

Changing defaultValue won't change the input's value on consequent re-render, because defaultValue is like an initial value once it's set it won't change between renders.

Here's a codesandbox demo for understanding

Solutions:

anuraghazra commented 12 months ago

Closing this as resolved, confirmed by @amandal97