Uncaught (in promise) TypeError: mostrarError is not a function. with hooks

Viewed 446

The function "mostrarError" come from props and I put it into the function "handleImagenSeleccionada" but when it´s used in the "catch(error)", it´s displayed the next error: Uncaught (in promise) TypeError: mostrarError is not a function.

export default function LoadImage({ mostrarError }) {
const [imagenURL, setImagenURL] = useState('');//URL gotten from the backend when the image was loaded in the server
const [subiendoImagen, setSubiendoImagen] = useState(false);// For the loading


//------------------------ Functions ---------------------------------
async function handleImagenSeleccionada(evento) {
    try {
        setSubiendoImagen(true);
        const file = evento.target.files[0];
        const formData = new FormData();
        formData.append('image', file);

        const { data } = await Axios.post(baseURL + '/inside/postImage', formData, { headers: { "Content-type": "multipart/form-data" } });
        setImagenURL(data.url);
        setSubiendoImagen(false);
    } catch (error) {
        setSubiendoImagen(false);
        console.log(mostrarError);
        mostrarError(error.response.data.message);

    }
}
return (
    //Form in JSX
);

}

I used console.log to show "mostrarError" and the cosoles shows me that "mostrarError" is a function. enter image description here

1 Answers

Your console log output shows mostrarError to be an object containing a function called mostrarError, i.e:

{
  mostrarError: function () {...}
}

Either use mostrarError.mostrarError when calling it, or (probably the better solution) make sure you're passing a function to the mostrarError prop,

Related