I have an react state with objects/array that is filled, but also then later I want to edit text inputs and edit the related fields on object also, i found no solutions until now.
So this is my state for example:
const [data, setData] = useState({
working_hours: [
{
id: 1,
description: 'Random Text',
price: 100,
},
{
id: 2,
description: 'Text Random',
price: 100,
},
]
});
Here is my Jsx:
{data.working_hours.map(item =>
<>
<input type={text} value={item.description}
onChange={(e) => handleChange(e)} />
<input type={text} value={item.price}
onChange={(e) => handleChange(e)} />
</>
)}
Here is what I tried:
function handleChange(e){
const value = e.target.value;
setData({...data, [...data.working_hours, e.target.value]})
}
>Solution :
You need to pass additional parameters to your handleChange as item ID which you want to update and property name because without these you will not be able to identify which property to update dynamically. This way you can use the same handleChange for multiple inputs.
See below code –
function handleChange(e, itemId, property) {
const value = e.target.value;
//copying data to temp variable so that we do not directly mutate original state
const tempWorkingHours = [...data.working_hours];
//findIndex to find location of item we need to update
let index = tempWorkingHours.findIndex(item => item.id == itemId);
// -1 check to see if we found that object in working hours
if(index != -1){
tempWorkingHours[index] = {
...tempWorkingHours[index], //keeping existing values in object
[property]: value //here property can be "price" or "description"
}
}
setData({ ...data, working_hours: tempWorkingHours })
}
{
data.working_hours.map(item =>
<>
<input type={text} value={item.description}
onChange={(e) => handleChange(e, item.id, "description")} />
<input type={text} value={item.price}
onChange={(e) => handleChange(e, item.id, "price")} />
</>
)
}