How to check if the control is hidden due to rules applied or due to form unload

I have created a control using the following uiSchema

{
                      scope: '#/properties/is_there_a_loan_on_this_property',
                      type: 'Control',
                      options: {
                        format: 'radio'
                      }
                    },
                    {
                      scope: '#/properties/navigation_loans',
                      type: 'Control',
                      rule: {
                        effect: 'SHOW',
                        condition: {
                          scope:
                            '#/properties/is_there_a_loan_on_this_property',
                          schema: {
                            enum: ['Yes']
                          }
                        }
                      }
                    }

In navigation_loans control I check for the visibility of the control and apply default data
to it if it’s not visible.

 useEffect(() => {
    setTimeout(() => {
      if (visible && !data) {
        const defaultData = {
          isEnabled: true,
          isRequired: true,
          isCompleted: false
        }
        handleChange(path, defaultData)
      }
      if (!visible) {
        const defaultData = {
          isEnabled: false,
          isRequired: false,
          isCompleted: true
        }
        handleChange(path, defaultData)
      }
    }, 0)
  }, [visible, data])

My problem is I don’t have a way to identify if the control is hidden due to the rule condition being met or if the form is being unloaded. I don’t want to update the navigation_loans value if the form is unloaded.

Hi @pankaj-z,

Sadly it’s not possible to identify within your control why you’re hidden.

I would like to recommend not tying the rendering code to data manipulation as was done in the posted code. The problem with this is that you will always run into weird edge cases and for example require the rendering of the form to even fill in your default values.

Instead I would like to recommend to fill in the defaults either before the form is rendered or afterwards before the entered data is further processed. If you require to fill in defaults at runtime then this could be handled in the update loop, e.g. as soon as the default data shall be filled in, then modify the data and hand over the new data to JSON Forms, i.e. modify the data outside of JSON Forms instead of within it.