osstotalsoft / rocket-ui-ts

A collection of reusable and composable React components written in TypeScript built on top of Material UI Core V5
MIT License
19 stars 4 forks source link

Get HTML Event On Change Callback #40

Closed dragos-rosca closed 1 year ago

dragos-rosca commented 1 year ago

Latest version

Description

I would like to have the original HTML event in my onChange callback. I need to read some data from the event, like the name of the component.

Examples

I need to make a single change callback for all the text fields in a form and for this I need to know the name of the property to update.

const handleChange = useCallback((value)=>{
  const name = // just guess the name of the field or some magic
  setFormData(prev =>({...prev, [name]:value})
},[])

This name can be found in the event object. So I propose that we get the original event as the second parameter like so:

const handleChange = useCallback((value, event)=>{
  const name = event.target.name
  setFormData(prev =>({...prev, [name]:value})
},[])