Question: Java script expert help code does not compile ( : import React, { useState } from 'react'; const DataForm = ( { addNewItem } )

Java script expert help code does not compile (:
import React, { useState } from 'react';
const DataForm =({ addNewItem })=>{
const [name, setName]= useState('');
const [brand, setBrand]= useState('');
const [price, setPrice]= useState('');
const [image, setImage]= useState(null);
const [error, setError]= useState(null);
const [success, setSuccess]= useState(false);
const handleImageChange =(e)=>{
const file = e.target.files[0];
if (file){
setImage(file); // Save the selected file in state
}
};
const handleSubmit = async (e)=>{
e.preventDefault();
// Validate input fields
if (!name ||!brand ||!price ||!image){
setError('All fields, including an image, are required');
setSuccess(false);
return;
}
const priceNumber = parseFloat(price);
if (isNaN(priceNumber)){
setError('Price must be a valid number');
setSuccess(false);
return;
}
// Create FormData object for submission
const formData = new FormData();
formData.append('name', name);
formData.append('brand', brand);
formData.append('price', priceNumber);
formData.append('gear', image);
setError(null);
try {
const response = await fetch('my api url', {
method: 'POST',
body: formData,
});
if (!response.ok){
throw new Error('Server error');
}
const data = await response.json();
if (data.success){
setSuccess(true);
addNewItem(data.newItem);
// Reset form
setName('');
setBrand('');
setPrice('');
setImage(null);
document.getElementById('fileInput').value =''; // Reset file input
} else {
setError(data.message || 'Failed to add the item');
setSuccess(false);
}
} catch (err){
setError('An error occurred while submitting the form');
setSuccess(false);
}
};
const handleUpdate = async (itemId, updatedData)=>{
try {
const response = await fetch(`my api url/${itemId}`,{
method: 'POST',
headers: { 'Content-Type': 'application/json'},
body: JSON.stringify(updatedData),
});
const data = await response.json();
if (data.success){
updateItem(data.updatedItem);
setSuccess(true);
} else {
setError(data.message || 'Failed to update the item');
setSuccess(false);
}
} catch (err){
setError('An error occurred while updating the item');
setSuccess(false);
}
};
const handleDelete = async (itemId)=>{
try {
const response = await fetch(`my api url/${itemId}`,{
method: 'DELETE',
});
const data = await response.json();
if (data.success){
deleteItem(itemId);
setSuccess(true);
} else {
setError(data.message || 'Failed to delete the item');
setSuccess(false);
}
} catch (err){
setError('An error occurred while deleting the item');
setSuccess(false);
}
};
return (
{error}Item added successfully!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!