I have a problem and I know what is but I can't solve it
I have the following select
<Select
defaultValue=""
labelId="surfaces-label"
id="surface_id"
name="surface_id"
value={surfaceId}
onChange={handleSurfaceChange}
label={translation.word.surface}
>
{surfaces.length > 0 ? (
surfaces.map((surface) => {
return (
<MenuItem value={surface.id} key={surface.created_at}>
{surface.name}
</MenuItem>
);
})
) : (
<MenuItem>-</MenuItem>
)}
</Select>
and this is loaded with data that I get from the API using the useEffect
useEffect(() => {
if (!surfaces.length) {
handleGetSurfaces();
}
}, []);
The problem is when I want to edit and I add the following code to useEffect
useEffect(() => {
if (!surfaces.length) {
handleGetSurfaces();
}
if (surfaceEditing) {
setSurfaceId(surfaceEditing.id);
validator.setFieldValue("surface_id", surfaceEditing.id);
}
}, []);
When is executed setSurfaceId(surfaceEditing.id); the data in the select doesn't exist yet because is getting from API and here is when I receive:
MUI: You have provided an out-of-range value
3for the select (name="surface_id") component. Consider providing a value that matches one of the available options or ''. The available values are "".
I need to setSurfaceId(surfaceEditing.id); after surface is loaded with .map but it doesn't work
