首页 > 解决方案 > Uncaught (in promise) TypeError: mostrarError 不是一个函数。带挂钩

问题描述

函数“mostrarError”来自道具,我将它放入函数“handleImagenSeleccionada”,但是当它在“catch(error)”中使用时,它显示下一个错误:Uncaught (in promise) TypeError: mostrarError is不是函数。

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
);

}

我使用console.log 来显示“mostrarError”,而cosoles 告诉我“mostrarError”是一个函数。 在此处输入图像描述

标签: reactjsreact-hooks

解决方案


您的控制台日志输出显示mostrarError为一个包含名为 的函数的对象mostrarError,即:

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

在调用它时使用mostrarError.mostrarError,或者(可能是更好的解决方案)确保将函数传递给mostrarError道具,


推荐阅读