Question: React component error import React, { useState } from 'react'; const DataForm = ( { addNewItem } ) = > { const [ name ,
React component error
import React, useState from 'react';
const DataForm addNewItem
const name setName useState;
const brand setBrand useState;
const price setPrice useState;
const error setError useStatenull;
const success setSuccess useStatefalse;
const handleSubmit async e
epreventDefault;
Clientside validation
if name brand price
setErrorAll fields are required';
return;
const priceNumber parseFloatprice;
if isNaNpriceNumber
setErrorPrice must be a valid number';
return;
try
const response await fetchurl goes here',
method: 'POST',
headers:
'ContentType': 'applicationjson
body: JSON.stringify name, brand, price: priceNumber
;
const data await response.json;
if datasuccess
setSuccesstrue;
addNewItemdatanewItem;
setName;
setBrand;
setPrice;
else
setErrorFailed to add the item';
catch err
setErrorAn error occurred';
;
return
errorItem added successfully!
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
