jsonforms icon indicating copy to clipboard operation
jsonforms copied to clipboard

Unpredictable data change

Open jankvapil opened this issue 1 year ago • 1 comments

Describe the bug

When I quickly click a button, sometimes the previous state is logged to the console. I can never be sure that I'm accessing actual form data (or at least I don't know how to get to them since there is no real reference to them or to the form)

const initialData = {
  name: 'Max Power',
};

const ClearFormExample = () => {
  const [data, setData] = useState(initialData);

  return (
    <div>
      <JsonForms
        schema={schema}
        uischema={uischema}
        data={data}
        renderers={materialRenderers}
        cells={materialCells}
        onChange={({ data, _errors }) => setData(data)}
      />
      <Button onClick={() => console.log(data)} color='primary'>
        Print data
      </Button>
    </div>
  );
};

Expected behavior

It would be nice to be able to directly access the internal state of the form by reference

Steps to reproduce the issue

  1. Just try the modified example from the docs

Screenshots

No response

Which Version of JSON Forms are you using?

v3.1.0

Framework

React

RendererSet

No response

Additional context

No response

jankvapil avatar Jan 25 '24 12:01 jankvapil

Hi @jankvapil,

In the React Material renderers we have two debounce mechanisms:

  • We debounce textual inputs like "string", "number" etc. within a 300ms time window. As these changes are debounced, they are not even part of the internal form wide data storage until they are applied
  • We also debounce React changes in general, but only with a time window of 10ms

You are likely hitting the first debounce. If you type fast and then click on a button. Obviously that's not great.

There are multiple improvements which can be done:

  • The debounce should be canceled and immediately applied when an input loses focus as we don't expect further input
  • Exposing the formwide state via ref makes sense to me
  • JSON Forms and/or especially debouncing renderers should indicate the debouncing state to the outside world, for example also via the ref. So clients can wait until the latest update is available.

Would you be interested in contributing one or more of these improvements?

sdirix avatar Jan 30 '24 09:01 sdirix