jeudi 27 janvier 2022

What pattern should I use in a React form that needs an item ID when submitting?

I have a web app that requires some crud functionality. In this specific case, the user needs to update an existing item in a database. Right now I have a form component that I tried to keep as "dumb" as possible, however, now I am not so sure if it is as "dumb" as it could be while still sticking to good React patterns.

I am passing in 3 props to my form:

  1. Existing values which I then set in state as the inputs are controlled
  2. ID of the item that user is updating
  3. Function called "onSubmit" which is essentially an API call to my database. It requires updated values and an ID as arguments

On form submit, I call my "onSubmit" function that has been passed in via props, which takes in the ID I have also passed in via props as well as the new values the user has selected via the inputs on the form.

Is this the common pattern to use in this situation?

Aucun commentaire:

Enregistrer un commentaire