'React MUI - form returns an variable that does not exist
I have a part of the form that should be filled with data and be editable at the same time (it is the Update of the CRUD). The object is initialized with useState as follows:
let defaultValues = {empresa_id:"",nombre_empresa: "",cif_empresa: "",actividad_empresa: "",calle_empresa: "",poblacion_empresa: "",cp_empresa: "",provincia_empresa: "",telefonos_empresa: [],emails_empresa: [],};
const [formValues, setFormValues] = useState(defaultValues);
So I send a contact to the component and the different values of the object are loaded into the inputs. For instance, one of them looks like this:
<TextField
key="editprnemp"
fullWidth
label="Provicia"
defaultValue={formValues?.provincia_empresa || ""}
name="provincia_empresa"
id="outlined-size-small"
size="small"
color="primary"
autoComplete="new-password"
onChange={handleInputChange}
/>
where handleInputChange is:
const handleInputChange = (e) => {
const { name, value } = e.target;
setFormValues({
...formValues,
[name]: value,
});
};
Now, inside the object, there are arrays which are phone numbers and emails. The user should be able to add as much of them as needed. Each of them has an identificator that will be used as label (ie. 'personal', 'home', 'mobile') and the value itself. Each of them has a own input so the user can write the identificator and the actual number or email.
{formValues?.telefonos_empresa?.map((inp, index)=>(
<>
<Autocomplete
disablePortal
freeSolo
autoSelect
fullWidth
value={inp.tel_identificador}
options={telSuggestions}
onChange={(event,value)=>anadirKey(event, value, index,formValues?.telefonos_empresa,setTelFormValues, "tel_identificador")}
id="telSuggestions"
size="small"
color="primary"
renderInput={(params) => <TextField {...params} label="Identificador teléfono" required/>}
isOptionEqualToValue={(option,value) => option === value}
getOptionLabel={ option => option}
/>
<TextField
label={<>Teléfono</>}
value={inp.tel}
onChange={(e)=>anadirValor(e,index,formValues?.telefonos_empresa,setTelFormValues, "tel")}
id="outlined-size-small"
size="small"
color="primary"
autoComplete="new-password"
onKeyUp={(e)=>anadirValor(e,index,formValues?.telefonos_empresa,setTelFormValues, "tel")}
required
/>
</>
))}
I have been using another useState in order to progress and be able to populate the arrays as I'm not sure how to do it.
const [telFormValues, setTelFormValues] = useState([]);
const [emailFormValues, setEmailFormValues] = useState([]);
const anadirValor= (e,index, array, setArr,arr_key_name)=>{
const lista = [...array];
console.log(JSON.stringify(lista));
var valorLimpiado = (e.target.value).trim();
valorLimpiado = valorLimpiado.replaceAll(/[:,]*/g,"");
lista[index][arr_key_name] = valorLimpiado;
setArr(lista);
var telmail ="";
arr_key_name === "tel" ? telmail="telefonos_empresa" : telmail="emails_empresa";
setFormValues({...formValues, [telmail]: array});
//and similar function for the key
But for some reason when I send the data using fetch, I get an error saying:
TypeError: _formValues$telefonos.map is not a function
which is strange because I have absolutely no value or variable called "telefonos" as it shows. I have searched this word in the document and there is no such thing. The most aproximately variable is "telefonos_empresa". Here the code breaks execution, I'm unable to find the reason behind the error. it says it is the line of autoselect
, which can be found in the code above that reads:
<Autocomplete
disablePortal
freeSolo
autoSelect
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|