I have few inputs in component and each of them have onChange to change their value. How can i simplify this code so i don’t write x similar functions? If item will have 6 inputs so I will have to copy this functions 6 times and change only one word in it (item.x) which looks pointless but I cant figure out how to make it better.
onChange functions:
const changeItemName = (event: any, id: string ) => {
const newItems: any = [...items]
const item: any = newItems.find((item:any) => item.id === id);
item.name = event.target.value;
setItems(newItems);
}
const changeItemDescription = (event: any, id: string ) => {
const newItems: any = [...items]
const item: any = newItems.find((item:any) => item.id === id);
item.description = event.target.value;
setItems(newItems);
}
Inputs:
<input
type='text'
name="name"
className='item-info-text'
placeholder='Nazwa przedmniotu'
maxLength={255}
value={item.name}
onChange={(e) => changeItemName(e, item.id)}
/>
<input
type='text'
name="description"
className='item-info-text'
placeholder='Opis przedmniotu'
maxLength={255}
value={item.description}
onChange={(e) => changeItemDescription(e, item.id)}
/>
5
Answers
Solved it by adding [event.target.name] to item:
You can create a single function and n pass the name of the property you want to change as a parameter:
You could create a dynamic
onChange
function that uses theinput
‘sname
to set it’svalue
on a object where you keep all the input valuesI think you can simply create one onChange handler in which you can set the values dynamically
it will store all the fields value in the items array
Witam!
To simplify your code and avoid writing multiple similar functions for each input, you can create a single function that takes an additional parameter to determine which property of the item should be updated.
i.e:
This function takes an additional
property
argument, which specifies which property of the item should be updated. You can now use this single function for both inputs by passing the respective property names:With this approach, you can easily handle multiple inputs with the same function by just passing the appropriate property name to the
changeItemProperty
function. This will make your code more concise and easier to maintain.