Answer the question
In order to leave comments, you need to log in
Is it possible to set an id to a Material UI component and refer to it?
I am using Material UI React in a project. And I decided to set the FieldText id='firstName' component and use it. When clicked, the id is de2e8a5b-7aab-4cb4-91bf-31817b6307e6.
Component example:
<Box className={classes.boxContainer}>
<TextField
id='firstName'
error={firstName !== '' && !firstNameValidationResult.valid}
helperText={firstNameValidationResult.errors[0]}
onChange={(event) => {
return test(event, id);
}}
margin='normal'
label={ETitleUser.firstName}
inputProps={{ maxLength: 20 }}
defaultValue=''
/>
<TextField
id='dept'
error={dept !== '' && !deptValidationResult.valid}
helperText={deptValidationResult.errors[0]}
onChange={() => {
return handlerOnChange;
}}
margin='normal'
label={ETitleUser.dept}
inputProps={{ maxLength: 20 }}
defaultValue=''
/>
</Box>
const handlerOnChange = (
event: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>,
id: string
) => {
switch (id) {
case 'firstName':
setFirstName(event.target.value);
break;
case 'lastName':
setLastName(event.target.value);
break;
case 'dept':
setDept(event.target.value);
break;
case 'login':
setLogin(event.target.value);
break;
}
};
Answer the question
In order to leave comments, you need to log in
If either for some reason puts its id on the element, then for some reason it needs this id. Rewriting it is a bug. Liba most likely provides his own way to trace the source of the event.
...upd: I checked - it will normally pass the id as it is, your problem is something else:
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question