Hello, I think instead of using splice, which doesn’t trigger a re-render, you can create a new array with the updated values. Use the onChange event to update the specific index in your state. When submitting the form, the data will be automatically structured as an array since you're maintaining it as such in the state.
Here's the updated code:
javascript
Copy import React, { useState } from 'react'; import { useForm } from '@inertiajs/inertia-react'; import { usePage } from '@inertiajs/inertia-react';
const StaffEducation = () => { const user = usePage().props.auth.user; const { data, setData, post, processing, errors } = useForm({ staffid: user.username, qualification: [''], institution: [''], course: [''], date_of_graduation: [''], });
const [educations, setEducations] = useState([0]);
const handleAddEducation = () => {
setEducations([...educations, educations.length]);
setData('qualification', [...data.qualification, '']);
setData('institution', [...data.institution, '']);
setData('course', [...data.course, '']);
setData('date_of_graduation', [...data.date_of_graduation, '']);
};
const handleRemoveEducation = (index) => {
setEducations(educations.filter((_, i) => i !== index));
setData('qualification', data.qualification.filter((_, i) => i !== index));
setData('institution', data.institution.filter((_, i) => i !== index));
setData('course', data.course.filter((_, i) => i !== index));
setData('date_of_graduation', data.date_of_graduation.filter((_, i) => i !== index));
};
const handleInputChange = (index, field, value) => {
const updatedArray = [...data[field]];
updatedArray[index] = value;
setData(field, updatedArray);
};
const handleSubmit = (e) => {
e.preventDefault();
post('/your-submit-route'); // update this with your actual route
};
return (
<>
<h5 className="card-title text-center">Education and Qualifications</h5>
<form onSubmit={handleSubmit}>
<table className="table">
<thead>
<tr>
<th>#</th>
<th>Qualification</th>
<th>University/College</th>
<th>Course/Programme</th>
<th>Date of Graduation</th>
<th>Action</th>
</tr>
</thead>
<tbody>
{educations.map((_, index) => (
<tr key={index}>
<th scope="row">{index + 1}</th>
<td>
<input
type="text"
className="form-control"
placeholder="e.g B.Sc"
value={data.qualification[index] || ''}
onChange={(e) => handleInputChange(index, 'qualification', e.target.value)}
/>
</td>
<td>
<input
type="text"
className="form-control"
placeholder="University/Institution"
value={data.institution[index] || ''}
onChange={(e) => handleInputChange(index, 'institution', e.target.value)}
/>
</td>
<td>
<input
type="text"
className="form-control"
placeholder="Course"
value={data.course[index] || ''}
onChange={(e) => handleInputChange(index, 'course', e.target.value)}
/>
</td>
<td>
<input
type="date"
className="form-control"
value={data.date_of_graduation[index] || ''}
onChange={(e) => handleInputChange(index, 'date_of_graduation', e.target.value)}
/>
</td>
<td>
{index === 0 ? (
<button type="button" className="btn btn-primary" onClick={handleAddEducation}>
Add
</button>
) : (
<button type="button" className="btn btn-danger" onClick={() => handleRemoveEducation(index)}>
Remove
</button>
)}
</td>
</tr>
))}
</tbody>
</table>
<button type="submit" className="btn btn-success" disabled={processing}>
Submit
</button>
</form>
</>
);
};
export default StaffEducation;