Question: Hello. Would just like the two pieces of code to work together. Have tried just about everything but still does do it . Please, please
Hello. Would just like the two pieces of code to work together. Have tried just about everything but still does do it Please, please help. Thank you much.
React component DataForm
import React, useState from 'react';
const DataForm addNewItem, updateItem, deleteItem
const name setName useState;
const brand setBrand useState;
const price setPrice useState;
const image setImage useStatenull;
const error setError useStatenull;
const success setSuccess useStatefalse;
const handleImageChange e
setImageetarget.files;
;
const handleSubmit async e
epreventDefault;
if name.trimbrand.trimprice.trimimage
setErrorAll fields are required, including an image.;
setSuccessfalse;
return;
const formData new FormData;
formData.appendname name;
formData.appendbrand brand;
formData.appendprice price;
formData.appendimage image;
try
const response await fetchMy URL',
method: 'POST',
body: formData,
;
const data await response.json;
if datasuccess
setSuccesstrue;
addNewItemdatanewItem;
setName;
setBrand;
setPrice;
setImagenull;
else
setErrordatamessage 'Failed to add the item.;
setSuccessfalse;
catch err
setErrorAn error occurred while submitting the form.;
setSuccessfalse;
;
return
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
