Form - resetting form into its initial state

1 Answer 68 Views
Form
Jan
Top achievements
Rank 1
Jan asked on 10 Feb 2022, 06:53 AM
Hi. I'm trying reset a form according to the example on https://www.telerik.com/kendo-react-ui/components/form/advanced-scenarios/ , as shown under "Resetting the Form Initial State". However, it does not work form me if I want to reset it with an object with the same data.

My scenario is that a user loads a form with initial data, fills in the form, submits the form. After the submit, we would like the form to revert to its original state (initial data). This does not happen as JSON.stringify produces the same key. I sorted it by adding an extra space into one property but I am sure there must be a better way to achieve this. 

  // initial data
  const [user, setUser] = React.useState({
    firstName: "John",
    lastName: "Smith",
    email: "John.Smith@email.com",
  });
  // data to reset
  const loadNewUser = () => {
    setUser({
      firstName: "John",
      lastName: "Smith",
      email: "John.Smith@email.com",
    });
  };

1 Answer, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 10 Feb 2022, 07:27 AM

Hello,

Thank you for the details.

In that case, we can suggest updating the key on submit. Then when we load the original value back it will be a new key. The approach that you have shared is also valid. The main goal is to change the key when we have to reset the Form. That is how React is re-mounting components internally.

Regards,
Stefan
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Jan
Top achievements
Rank 1
commented on 11 Feb 2022, 12:12 PM

Thank you very much for the solution, much appreciated.  It works just fine now. 

With kind regards, 

Jan

Tags
Form
Asked by
Jan
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Share this question
or