Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unpredictable data change #2260

Open
jankvapil opened this issue Jan 25, 2024 · 1 comment
Open

Unpredictable data change #2260

jankvapil opened this issue Jan 25, 2024 · 1 comment

Comments

@jankvapil
Copy link

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

@sdirix
Copy link
Member

sdirix commented Jan 30, 2024

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 sdirix added this to the next milestone Jan 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants